Author Topic: TableView Column Sort  (Read 4220 times)

jclevien

  • Newbie
  • *
  • Posts: 4
TableView Column Sort
« on: May 17, 2008, 09:59:28 AM »
Hello,

My name is Juan. I am a Gambas newbie, made a program with it in 3 days (where the same program in GTK + Python would have taken 1 week, by the way), and made a little code I didn't find anywhere, so I am posting it for your pleasure (and testing... seems to work OK, but....) :)

Code: [Select]

PUBLIC FUNCTION Search(tv AS TableView, i AS Integer, col AS Integer, Mode AS Integer) AS Integer
 
  DIM k AS Integer
  DIM pos AS Integer
 
  DIM v1 AS Variant
  DIM v2 AS Variant
 
  pos = i
 
  FOR k = i TO tv.Rows.Count - 1
    IF IsNumber(Val(tv[k, col].Text)) THEN
      'Behaves like number
      v1 = Val(tv[k, col].Text)
      v2 = Val(tv[pos, col].Text)

      IF v1 = NULL THEN
        v1 = 0
      END IF
     
      IF v2 = NULL THEN
        v2 = 0
      END IF

      IF mode = 0 THEN 'Min
        IF CFloat(v1) < CFloat(v2) THEN
          pos = k
        END IF
      ELSE 'Max
        IF CFloat(v1) > CFloat(v2) THEN
          pos = k
        END IF
      END IF

     
    ELSE
      'Behaves like string
     
      v1 = tv[k, col].Text
      v2 = tv[pos, col].Text
     
      IF v1 = NULL THEN
        v1 = " "
      END IF
     
      IF v2 = NULL THEN
        v2 = " "
      END IF     

      IF mode = 0 THEN 'Min
        IF v1 < v2 THEN
          pos = k
        END IF
      ELSE 'Max
        IF v1 > v2 THEN
          pos = k
        END IF
      END IF


    END IF

  NEXT
 
  RETURN pos
 
END


PUBLIC SUB Change(tv AS TableView, i AS Integer, pos AS Integer)
 
  DIM k AS Integer
  DIM row AS NEW String[]
 
  'Load first data into row
 
  FOR k = 0 TO tv.Columns.Count - 1
    row.Add(tv[i, k].Text)
  NEXT

  'Put second row into first
 
  FOR k = 0 TO tv.Columns.Count - 1
    tv[i, k].Text = tv[pos, k].Text
  NEXT
   
  'Finally, save temporary data
 
  FOR k = 0 TO tv.Columns.Count - 1
    tv[pos, k].Text = row[k]
  NEXT
   
 
END





PUBLIC SUB SortTableView(tv AS TableView, col AS Integer, A AS Boolean)
 
  DIM i AS Integer
  DIM MinRes AS Integer
  DIM MaxRes AS Integer
  DIM row AS String[]
 
  tv.Cancel()
 
  FOR i = 0 TO tv.Rows.Count - 1
    IF A THEN

      MinRes = Search(tv, i, col, 0)
      Change(tv, i, MinRes)

    ELSE
      MaxRes = Search(tv, i, col, 1)
      Change(tv, i, MaxRes)

    END IF
 
  NEXT
 
  tv.Refresh
 
END


PUBLIC SUB fgGrid_ColumnClick(Column AS Integer)
 
  SortTableView(fgGrid, Column, TRUE)
 
 
END






As you can see, you need to call 'SortTableView', and takes the TableView, the Column number you want to sort out, and TRUE if you want ascending order or FALSE with descending order.

The ordering algorithm is the selection one (pick the minimum (or maximum) and interchange it with the first element.

Note that it is contemplated the type of the column; it detects if it is numeric, and sorts it as expected.

Hope you like it! (and if you find some bug/improvement, please tell me about it, so I can improve them).


Juan

Linux Basic

TableView Column Sort
« on: May 17, 2008, 09:59:28 AM »