Android Question SORTING ... Your Opinion Please.

rfhall50

Member
Licensed User
Longtime User
I need to set up a multi-element sort.
I have an array that contains <= 50 "rows". Each row will have 20 "columns".
Defined as MyArray(50,20)

Sample data could look like:
Name4, Category2, 4, 5, 7, ...
Name3, Category1, 7, 4, 9, ...
Name2, Category2, 8, 3, 4, ...
Name1, Category3, 5, 2, 1, ...
etc.

I want to sort the array first by Category, then by name. Final result should be:

Name3, Category1, 4, 5, 7, ...
Name2, Category2, 7, 4, 9, ...
Name4, Category2, 8, 3, 4, ...
Name1, Category3, 5, 2, 1, ...

Your suggestions for the best sorting technique to use ? If you have such an example, it would be appreciated. Thank You.

Bob Hall
 

sorex

Expert
Licensed User
Longtime User
SQL is ideal for multi sort but you could also try adding the items to a list as full field string and apply a sort on that list.

B4X:
lst.add("Name3, Category1, 4, 5, 7")
lst.add("Name2, Category2, 7, 4, 9")
lst.sort(true)

requires some splitting to get the data back but that easy and extra padding/prefixing might be needed to get it all sorting right.
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
what you are trying to do is not simple since you want to sort by 2 columns at once.

i dont know about sql but if you want to do it using b4x only then you should create types and then sort all items by column category first and then build a function where you add in a loop items with the same group to a new list sort them and add them back to list 1 until list 0 is empty

it could look like this (this is a b4j project but for b4a/b4i its the same):

B4X:
Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
   
    Type row(name As String, category As String, col1 As Int, col2 As Int, col3 As Int, col4 As Int, col5 As Int)'... add more columns as you need
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.SetFormStyle("UNIFIED")
    MainForm.Show
   
    Dim itemslist As List
    itemslist.Initialize
   
    'create list with random items
    For i = 0 To 10
        Dim newrow As row
        newrow.name = "Name" & NumberFormat(i,2,0)
        newrow.category = "Category" & Rnd(1,5)
        newrow.col1 = Rnd(0,9)
        newrow.col2 = Rnd(0,9)
        newrow.col3 = Rnd(0,9)
        newrow.col4 = Rnd(0,9)
        newrow.col5 = Rnd(0,9)
        itemslist.Add(newrow)
    Next
   
    Log("########## BEFORE SORTING ##############")
    For Each newrow As row In itemslist
        Log(newrow.name & ", " & newrow.category & ", " & newrow.col1 & ", " & newrow.col2 & ", " & newrow.col3 & ", " & newrow.col4 & ", " & newrow.col5)
    Next
   
    Dim sortedlist As List = sortplus(itemslist,"category","name") 'get the new sorted list back
   
    Log("########## AFTER SORTING ##############")
    For Each newrow As row In sortedlist
        Log(newrow.name & ", " & newrow.category & ", " & newrow.col1 & ", " & newrow.col2 & ", " & newrow.col3 & ", " & newrow.col4 & ", " & newrow.col5)
    Next
End Sub

Sub sortplus(l As List, col1 As String, col2 As String) As List
    Dim list1, list2, list3 As List
    list1.Initialize
    list2.Initialize
    list3.Initialize
   
    l.SortType(col1,True) 'first we sort the items by category
   
    For Each r As row In l
        If list1.IndexOf(r.category) = -1 Then
            list1.Add(r.category) 'add all categories that are available
        End If
    Next

    For Each item As String In list1
        For Each r As row In l
            If r.category = item Then
                list2.Add(r)
            End If
        Next   
        list2.SortType("name",True)
        list3.AddAll(list2)
        list2.Clear
    Next
    Return list3
End Sub

Log result:

Waiting for debugger to connect...
Program started.
########## BEFORE SORTING ##############
Name00, Category1, 5, 4, 3, 4, 5
Name01, Category1, 4, 0, 6, 0, 4
Name02, Category1, 1, 4, 3, 3, 7
Name03, Category1, 5, 2, 6, 4, 0
Name04, Category3, 1, 2, 0, 7, 8
Name05, Category4, 3, 8, 7, 3, 4
Name06, Category2, 8, 8, 0, 5, 2
Name07, Category3, 6, 6, 7, 7, 2
Name08, Category2, 8, 7, 1, 6, 5
Name09, Category4, 2, 0, 0, 1, 3
Name10, Category3, 0, 4, 3, 3, 8
########## AFTER SORTING ##############
Name00, Category1, 5, 4, 3, 4, 5
Name01, Category1, 4, 0, 6, 0, 4
Name02, Category1, 1, 4, 3, 3, 7
Name03, Category1, 5, 2, 6, 4, 0
Name06, Category2, 8, 8, 0, 5, 2
Name08, Category2, 8, 7, 1, 6, 5
Name04, Category3, 1, 2, 0, 7, 8
Name07, Category3, 6, 6, 7, 7, 2
Name10, Category3, 0, 4, 3, 3, 8
Name05, Category4, 3, 8, 7, 3, 4
Name09, Category4, 2, 0, 0, 1, 3


if you want to save custom types then you should use KeyValueStore lib: https://www.b4x.com/android/forum/t...imple-powerful-local-datastore.63633/#content
 
Last edited:
Upvote 0
Top