Android Question List Sorting and number conversion very slow

Noelkman

Member
Licensed User
Longtime User
Erel, Klaus,
I'm using Klaus's wonderful Flexible Table View that uses string lists for holding the table data. I'm experencing big performance issues when doing a number sort. When converting a column to number format for comparison (double) it is very slow. String sorting ist very fast on the same table

'--o--o--o--o number sort snippet
'Dim s1 as Double = values1(col) 'strings list

This simple conversion is so slow (in a table of about 6000) items that I can not beleave that this is right.
Since my table for that column only contains integers I worked around it with leaving the strings as string and simply add leading zeros to the comparising variables which works great and almost same speed like sorting strings.

Now, here is my questions to Erel: Is there any other way for conversion string to number instead the one I described or is this a technical limitation?
 

Noelkman

Member
Licensed User
Longtime User
You are right Erel that was dumb!
Sorry about posting to individuals, I didn't think about that. Everyone is invited of course.

Yes, I have tested in release mode.
IMHO posting code is not needed. It is the number sorting routine from Klaus's Flexible table.
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
IMHO posting code is not needed

Posting code is always a good idea. If one of us has 5 minutes to look at an issue you may get a quick answer. But not if it involves downloading and installing libraries and/or writing a test app to do it. It will invariably help us to help you.
 
Upvote 0

Noelkman

Member
Licensed User
Longtime User
Posting code is always a good idea. If one of us has 5 minutes to look at an issue you may get a quick answer. But not if it involves downloading and installing libraries and/or writing a test app to do it. It will invariably help us to help you.

B4X:
    Dim minIndex As Int
    For i = 0 To Data.Size- 1
        minIndex = i
        For j = i + 1 To Data.Size - 1
            Dim values1() As String = Data.get(j)
            Dim s1 As Double = values1(col)
            Dim values2() As String = Data.get(minIndex)
            Dim s2 As Double = values2(col)
            If (dir) Then
                If s1 < s2 Then minIndex = j
            Else 
                If s1 > s2 Then minIndex = j
            End If
        Next
       SwapList(i, minIndex)
    Next

Here you go,
but like I said it is not my code and if I don't convert to numbers (s1, s2) this Bubblsort algorithm is quit fast with strings.
But then of course it is not sorted numberwise but ascii. What I did to workaround it I had described in the first post.
My method is pretty fast and I could live with it but for me it is nonsens.
So, the problem I see is the conversion fom string to number like "Dim s1 As Double = values1(col)"
Even when the Dim is outside the loop. I have also taken out the "If" operator for "dir" but no change.
 
Upvote 0

Noelkman

Member
Licensed User
Longtime User
Erel,
thanks for your example. At least it brought me to the right way. I don't know if you are aware of Klaus's Flexible Table Class (which is almost perfect) but the sorting ist way to slow. He stores the data in a list of string arrays. This makes it a bit complicated to sort for numbers. With your example I combine the class with my own sorting routines which is fast as lightspeed. I map out the data from the list of string arrays to user defined types and create a list from there. Sort it using the "list.SortType()" and put the sorted data back to the Table Class. I don't know what algo of sorting is used with this function but it is fast as hell even with mapping out the data and writing it back. I shoot the function and it is just done and shows the perfect sorted table. Crazy! I need to measure it but I think it is below a second with about 5600 rows and lots of string data.
Of course I could reload the data from sql whith different sorting direction but this is slower than sorting (doesn't need to be tested you simply see it).
Erel, do you know if this is the core java sorting that is used from Lists?
 
Upvote 0
Top