Android Question B4XTable sorting problem

hazaal

Member
Licensed User
Longtime User
Hi All,

I have B4XTable which has numbers stored as text (00001, 00002, etc.). I try to sort the column in code by setting InternalSortMode = "DESC", but for some reason it doesn't work

Please see below, left side is after in-code sort, right side some seconds later after header click

1693416396502.png


Same problem with ASC order, numbers are not in correct order.

What am I doing wrong?

Br,
-Harri
 

hazaal

Member
Licensed User
Longtime User
Calling...:
Sort(prjtable,prjtable.GetColumn("Nro")," DESC")

I believe this is copied from one of Erel's post:
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

I have applied sort in code only to one column.

Pls. note, when I click the header right after calling in code sub Sort, it works.

Br,
-Harri
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
I have applied sort in code only to one column.
Did you follow this protocol:
B4X:
B4XTable1.AddColumn("Nro", B4XTable1.COLUMN_TYPE_TEXT)
    Sleep(0)  'This line must be here after defining the col. It gives the internal layout time to load
    Sort(B4XTable1,B4XTable1.GetColumn("Nro")," DESC")
If this does not solve it for you and you are willing to post a small recreated project, we will get to the bottom of it.
 
Upvote 0

hazaal

Member
Licensed User
Longtime User
Did you follow this protocol:
B4X:
B4XTable1.AddColumn("Nro", B4XTable1.COLUMN_TYPE_TEXT)
    Sleep(0)  'This line must be here after defining the col. It gives the internal layout time to load
    Sort(B4XTable1,B4XTable1.GetColumn("Nro")," DESC")
If this does not solve it for you and you are willing to post a small recreated project, we will get to the bottom of it.
Thanks, but I guess I tried that already, and now again, no help.

Here is the code associated B4XTable and loading the data from JRDC server. Source database is sqllite which is converted automatically from very old dbase with python script when it notices file change.

B4X:
Private Sub load_projects_to_table
    Dim data As List
    Dim prono As String
    Dim prj As String
    data.Initialize
    prjtable.Clear
    Dim c As B4XTableColumn = prjtable.AddColumn(" Nro", prjtable.COLUMN_TYPE_TEXT)
    c.Width = 60dip
    Dim c As B4XTableColumn = prjtable.AddColumn(" Projekti", prjtable.COLUMN_TYPE_TEXT)
    c.Width = 600dip   
    Sleep(0)
    prjtable.HeadersHeight = 30dip
    prjtable.RowHeight = 23dip
    prjtable.DefaultDataFormatter.GetDefaultFormat.GroupingCharacter  = ""
    prjselections.Initialize(prjtable)
    prjselections.Mode = prjselections.MODE_SINGLE_LINE_PERMANENT
    
    Dim rs As ResumableSub = get_projects
    Wait For(rs) Complete (res As DBResult)
    
    For Each row() As Object In res.Rows
        prono = NumberFormat2(row(0), 5, 0, 0, False)
        prj = " " & row(1)
        data.Add(Array(prono, prj))
    Next

    Wait For (prjtable.SetData(data)) Complete (Unused As Boolean)

    SetColumnHorizontalAlignment(prjtable.GetColumn("Projekti") , "LEFT")
    Sort(prjtable,prjtable.GetColumn("Nro"),"DESC")
    
End Sub

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

Private Sub get_projects As ResumableSub
    Dim sqls As String
    If cbShowPassiiviset.Checked Then
        sqls = "SELECT_ALL_FROM_PROJECTS"
    Else
        sqls = "SELECT_AKTIIVISET_FROM_PROJECTS"
    End If
    Dim com As DBCommand = CreateCommand(sqls,Null)
    Dim job As HttpJob = CreateRequest.ExecuteCommand(com, Null)
    Dim req As DBRequestManager = CreateRequest
    Wait For (req.ExecuteQuery(com, 0, Null)) JobDone (job As HttpJob)
    If job.Success Then
        req.HandleJobAsync(job, "req")
        Wait For (req) req_Result (res As DBResult)
        req.PrintTable(res)
    Else
        xui.MsgboxAsync(job.ErrorMessage, "Error while reading projects")
    End If
    
    job.Release
    Return res
    
End Sub
 
Upvote 0

hazaal

Member
Licensed User
Longtime User
Sorry, found it... stupid missing space in column name vs. ColumnId.

In other words, I have used space in the beginning of column header to get it better looking

B4X:
Dim c As B4XTableColumn = prjtable.AddColumn(" Nro", prjtable.COLUMN_TYPE_TEXT)

but NOT in get GetColumn for sorting

B4X:
Sort(prjtable,prjtable.GetColumn("Nro"),"DESC")

After adding the space, and changing sort order to ASC, it works nicely, and now I understand B4XTable a bit more, not much though, but a bit more...

Sorry for troubling, too much wasted time for this! Unfortunately it's not the first time, nor last, I have been spending time for something as stupid as this.
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
Sorry, found it... stupid missing space in column name vs. ColumnId.
It happens. It is common. That's why a project attached or pertinent code would help. Nobody would have been able to help you without seeing code or project in this case Good thing you sorted it out. I also noticed you did the same thing with this: prjtable.AddColumn(" Projekti", prjtable.COLUMN_TYPE_TEXT). The closest thing to using a space ( although I do not use a leading space in my headers) if you want to is to use an underscore which is more noticeable.
 
Upvote 0
Top