sorting data in a listbox

gjoisa

Active Member
Licensed User
Longtime User
Hi all !
I am confused about sorting data in ascending mode in a listbox . Can any one help ?
 

klaus

Expert
Licensed User
Longtime User
There is no Sort property or method for ListBox on the device.
There is one for the desktop, but it needs the Door library.
You can first put your data in an ArrayList, sort it and then transfer the sorted data to the ListBox.

Best regards.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
And here is a code that implements klaus suggestion:
B4X:
Sub Globals
    'Declare the global variables here.

End Sub

Sub App_Start
    Form1.Show
    SortListBox("ListBox1")
End Sub

Sub SortListBox(List)
    ArrayList1.Clear
    For i = 0 To Control(List,ListBox).Count-1
        ArrayList1.Add(Control(List,ListBox).Item(i))
    Next
    ArrayList1.Sort(cNumbers)
    Control(List,ListBox).Clear
    For i = 0 To ArrayList1.Count-1
        Control(List,ListBox).Add(ArrayList1.Item(i))
    Next
End Sub

It uses an ArrayList named ArrayList1 to sort listboxes. Depending on your type of data you may need to change the line:
B4X:
ArrayList1.Sort(...)
 
Top