B4J Question How to emulate B4XTable column title click to sort its content ascending or descending using code?

Laurent95

Active Member
Licensed User
Give more informations. On what is based your B4XTable a SQL request, a list ?
Not easy to help you with no information or code.
 

JG Paramo

Member
Licensed User
1615993094952.png


I am figuring-out the code on how to sort that B4XTable columns "Group" or "Group Rating" ascending or descending using code instead of clicking that column heading, but sadly I'm stuck. The table is being populated using SetData from a list fetched from a SQLite database. Been searching for quite a while in this forum but haven't seen similar codes or any code snippets, even samples from Erel I didn't found any, or maybe I am just unaware of the B4XTable methods that can do this.. Thanks.
 
Last edited:

Laurent95

Active Member
Licensed User
I am figuring-out the code on how to sort that B4XTable columns "Group" or "Group Rating" ascending or descending using code instead of clicking that column heading, but sadly I'm stuck. The table is being populated using SetData from a list fetched from a SQLite database. Been searching for quite a while in this forum but haven't seen similar codes or any code snippets, even samples from Erel I didn't found any, or maybe I am just unaware of the B4XTable methods that can do this.. Thanks.
Ok,
I don' know well the B4XTable, but in a project i use tableview who's probably nearest to use.
And indeed i never found a solution to know what's the column who's sorted or not.
My solution maybe not the fastest, depends the size of data, but here i use it on 3 years of rates exchange need less than 1min.
I refresh the tableview with data from a list sorted, it's easy to sort data form a list ascending or not
An example more easy to understand where i use for the min and maxe rate exchange on this application in B4J:
Example for lists sorted:
            Dim SQLRequest As String =  "SELECT Date, Rate, DateLong FROM Currencies WHERE Currency='PHP' AND DateLong >=" & DatePicker1.DateTicks & " AND DateLong <=" & DatePicker2.DateTicks         'Used max 3 years, a bit more long :)
            lst = DBUtils.ExecuteMemoryTable(mySQL, SQLRequest, Null, 0)
            If (lst.Size > 0) Then
                Dim lstMin, lstMax As List
                lstMin.Initialize
                lstMax.Initialize
                For i = 0 To lst.Size - 1
                    Dim result() As String = lst.Get(i)
                    lstMin.Add(SingleLine1.NumberFormat3(result(1),2))
                    lstMax.Add(SingleLine1.NumberFormat3(result(1),2))
                Next
                lstMin.Sort(True)        'Sort ascending
                lstMax.Sort(False)        'Sort unascending
                Log("Minimum rate: " & lstMin.Get(0))        'So the min is index 0
                Log("Maximum rate: " & lstMax.Get(0))        'So the max is index 0 also
        'That's all
[EDIT]
When i see your Table i think your list is based on a Map where the records are stored with a type.
Maps are more easy to sort, you can even choose the key to sort on any column that you want ;)
No example under the hand but it's easy to find on the Forum

Hope that'll help you.
Good coding,
Laurent
 
Last edited:

JG Paramo

Member
Licensed User
Ok,
I don' know well the B4XTable, but in a project i use tableview who's probably nearest to use.
And indeed i never found a solution to know what's the column who's sorted or not.
My solution maybe not the fastest, depends the size of data, but here i use it on 3 years of rates exchange need less than 1min.
I refresh the tableview with data from a list sorted, it's easy to sort data form a list ascending or not
An example more easy to understand where i use for the min and maxe rate exchange on this application in B4J:
Example for lists sorted:
            Dim SQLRequest As String =  "SELECT Date, Rate, DateLong FROM Currencies WHERE Currency='PHP' AND DateLong >=" & DatePicker1.DateTicks & " AND DateLong <=" & DatePicker2.DateTicks         'Used max 3 years, a bit more long :)
            lst = DBUtils.ExecuteMemoryTable(mySQL, SQLRequest, Null, 0)
            If (lst.Size > 0) Then
                Dim lstMin, lstMax As List
                lstMin.Initialize
                lstMax.Initialize
                For i = 0 To lst.Size - 1
                    Dim result() As String = lst.Get(i)
                    lstMin.Add(SingleLine1.NumberFormat3(result(1),2))
                    lstMax.Add(SingleLine1.NumberFormat3(result(1),2))
                Next
                lstMin.Sort(True)        'Sort ascending
                lstMax.Sort(False)        'Sort unascending
                Log("Minimum rate: " & lstMin.Get(0))        'So the min is index 0
                Log("Maximum rate: " & lstMax.Get(0))        'So the max is index 0 also
        'That's all
[EDIT]
When i see your Table i think your list is based on a Map where the records are stored with a type.
Maps are more easy to sort, you can even choose the key to sort on any column that you want ;)
No example under the hand but it's easy to find on the Forum

Hope that'll help you.
Good coding,
Laurent
Thanks on this Laurent, B4XTable has a Sort method similar with that for lists that I may have to explore. Cheers..
 

Erel

Administrator
Staff member
Licensed User
B4X:
'SortMode - ASC or DESC
Sub Sort (Table As B4XTable, Column As B4XTableColumn, SortMode As String)
        For Each c As B4XTableColumn In Table.Columns
            If c = Column Then
                c.InternalSortMode = SortMode
            Else
                c.InternalSortMode = ""
            End If
        Next
        Table.FirstRowIndex = 0
        Table.Refresh
End Sub
 
Last edited:

JG Paramo

Member
Licensed User
B4X:
'SortMode - ASC or DESC
Sub Sort (Table As B4XTable, Column As B4XTableColumn, SortMode As String)
        For Each c As B4XTableColumn In Table.Columns
            If c = Column Then
                If c.InternalSortMode = SortMode
            Else
                c.InternalSortMode = ""
            End If
        Next
        Table.FirstRowIndex = 0
        Table.Refresh
End Sub
It worked, after removing the "If" (maybe a typo error) from "If c.InternalSortMode = SortMode" .. Thank you Erel!
 
Top