B4J Question Tableview sort

Daestrum

Expert
Licensed User
Longtime User
look at
table.Items.sort

Didn't realize it existed
 
Last edited:
Upvote 0

davelew1s

Active Member
Licensed User
Longtime User
Hi Daestrum! I have tried that and it only gives a error ...also you cannot select a column. I also tried sort type but could not get that to work either.
Thanks Dave.
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
I must admit, I couldn't get it to work either after many attempts.
You can try this method, modify it to suit your needs. It sorts on column number.
It uses a bubble sort, which would be slow for a large table.
tableshellSort.B4J implements a shell sort on the table, much faster,but only sorts numeric columns

As the sort runs on the main thread, sorting a table > 200 items can cause display to not show the table until sort is finished.
It may be better to fire the mouse_clicked event on the column header and let that sort the table, as it seems to do it so much faster.
 

Attachments

  • tablesort.zip
    1.5 KB · Views: 293
  • tableshellsort.zip
    1.7 KB · Views: 251
Last edited:
Upvote 0

davelew1s

Active Member
Licensed User
Longtime User
Hi Daestrum! Thanks I tried both your suggestions and they work I only have 20 -40 rows at any one time so any delay is not a problem.
I think I will go with tableshellSort.B4J. Not what you mean by "fire the mouse_clicked event on the column header and let that sort the table"
can you explain?
Dave.
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
On the tableview - if you click a heading, providing the column is set to sort-able, then it goes through 3 sorts. None, Ascend and Descend.
I thought it may be possible to fabricate the mouse_cllick event, to cause the sort to occur. So far I have been unsuccessful in getting it to work.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Another solution based on List.SortType:
B4X:
Sub Process_Globals
   Private fx As JFX
   Private MainForm As Form
   Private TableView1 As TableView
   Type TableHelper (SortColumn As Object, TableRow() As Object)
End Sub

Sub AppStart (Form1 As Form, Args() As String)
   MainForm = Form1
   MainForm.RootPane.LoadLayout("1") 'Layout with a table named TableView1
   MainForm.Show
   TableView1.SetColumns(Array As String("col1", "col2", "col3"))
   For i = 1 To 500
     TableView1.Items.Add(Array As Object("Item " & i, Rnd(0, 100), Rnd(0, 100)))
   Next
   SortTable(TableView1, 1)
End Sub

Sub SortTable(tv As TableView, ColumnIndex As Int)
   Dim list1 As List
   list1.Initialize
   For Each row() As Object In tv.Items
     Dim th As TableHelper
     th.SortColumn = row(ColumnIndex)
     th.TableRow = row
     list1.Add(th)
   Next
   list1.SortType("SortColumn", True)
   tv.Items.Clear
   For Each th As TableHelper In list1
     tv.Items.Add(th.TableRow)
   Next
End Sub

It takes 250ms to sort the table with 100,000 rows (3 columns).
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
Thank you Erel for the explanation, I think what was confusing me was the fact sorttype etc, is shown in the drop down for the table. It makes sense when applied to a list.
Once again thanks for the example of it's use.

Out of interest, is the click 'the header sort' faster than table > list > sort > table ?
I know its probably hard to time the sort done by the table itself.
 
Last edited:
Upvote 0
Top