I have a list filled with same type of maps.
Need to sort list by the any key value of maps.
I was trying to write a function for this and this sample code is working but I'am curious about that is there another way more simple or fast?
Need to sort list by the any key value of maps.
I was trying to write a function for this and this sample code is working but I'am curious about that is there another way more simple or fast?
B4X:
public Sub SortMapListByKeyValue(mapList As List, keyName As String, ascending As Boolean) As List
Dim keyValList As List
keyValList.Initialize
For Each m As Map In mapList
keyValList.Add(m.Get(keyName))
Next
keyValList.Sort(ascending)
Dim newList As List
newList.Initialize
For i=0 To keyValList.Size-1
For j=0 To mapList.Size-1
Dim m As Map = mapList.Get(j)
If m.Get(keyName)=keyValList.Get(i) Then
newList.Add(m)
mapList.RemoveAt(j)
Exit
End If
Next
Next
Return newList
End Sub