iOS Code Snippet Sort a list by reference to a second list

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:

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:

Marroq Spoulinker

Member
Licensed User
Longtime User
@stevel05 - thank you for providing this Code Snippet.

After searching how to do this and finding this post, I spent some time working to implement your solution before realizing that the built-in List.SortType functionality (introduced in ver. 1.2) works much better for my needs.

I wanted to post this here so that anyone doing the same search will know that List.SortType is also available.

https://www.b4x.com/android/forum/pages/results/?query=list.sorttype&page=1&prefix=0
 
Top