SubName: SortListByList
Description: I am writing an input routine where the user can select multiple items by touching images on the screen. Any selection they make will be valid, but they only make sense if they are in the correct order. The output order is not sequential, so can't be sorted directly. The Lists contain Strings.
I wanted to sort their selections by reference to a list that has the correct order. Writing an input validation would be tricky, and potentially frustrating to the user if they get the sequence wrong. So I decided to let them enter the selections in any order and sort them afterwards.
The list of possible selections is not large, so the time taken to sort the list wont be significant.
This is the code I came up with:
Usage:
A quick and dirty fix to the problem without having to change the data structure.
The reference list must contain all possible selections, otherwise the selection will get lost, duplicate items in the list being sorted will be ignored.
This second version allows and returns duplicates from the ListToSort in the returned list.
I hope it helps someone.
Tags: Sort List Reference Using List
Description: I am writing an input routine where the user can select multiple items by touching images on the screen. Any selection they make will be valid, but they only make sense if they are in the correct order. The output order is not sequential, so can't be sorted directly. The Lists contain Strings.
I wanted to sort their selections by reference to a list that has the correct order. Writing an input validation would be tricky, and potentially frustrating to the user if they get the sequence wrong. So I decided to let them enter the selections in any order and sort them afterwards.
The list of possible selections is not large, so the time taken to sort the list wont be significant.
This is the code I came up with:
B4X:
Sub SortListByList(ListToSort As List,Reference As List) As List
Dim M As Map
M.Initialize
Dim ReturnList As List
ReturnList.Initialize
'Add each item in the list to sort to a map
For Each Item As Object In ListToSort
M.Put(Item,"")
Next
'Iterate over the sorted reference list
For Each Item As Object In Reference
Dim ReturnedItem As Object = M.Get(Item)
'If there is an item in the map that matches
If ReturnedItem <> Null Then
'Add it to our returnlist
ReturnList.Add(Item)
End If
Next
Return ReturnList
End Sub
Usage:
B4X:
Dim SortedList As List = SortListByList(InputList,ReferenceList)
A quick and dirty fix to the problem without having to change the data structure.
The reference list must contain all possible selections, otherwise the selection will get lost, duplicate items in the list being sorted will be ignored.
This second version allows and returns duplicates from the ListToSort in the returned list.
B4X:
Sub SortListByListWithDuplicates(ListToSort As List,Reference As List) As List
Dim M As Map
M.Initialize
Dim ReturnList As List
ReturnList.Initialize
'Add each item in the list to sort to a map, and count the entries
For Each Item As Object In ListToSort
M.Put(Item,M.GetDefault(Item,0) + 1)
Next
'Iterate over the sorted reference list
For Each Item As Object In Reference
Dim ReturnedItemCount As Object = M.Get(Item)
'If there is an item in the map that matches
If ReturnedItemCount <> Null Then
'Add the counted entries to our returnlist
For i = 0 To ReturnedItemCount -1
ReturnList.Add(Item)
Next
End If
Next
Return ReturnList
End Sub
I hope it helps someone.
Tags: Sort List Reference Using List
Last edited: