Experimenting with various sort algorithms.
It looks it is not possible to access items of a B4XOrderedMap by index, as there is no GetValueAt method as in the regular map.
There is a way round that but it seems a bit convoluted and inefficient:
Would it be possible to add this method to the B4XOrderedMap?
RBS
It looks it is not possible to access items of a B4XOrderedMap by index, as there is no GetValueAt method as in the regular map.
There is a way round that but it seems a bit convoluted and inefficient:
B4X:
Sub SortOMStringIDX(arrString() As String, bCaseInsensitive As Boolean) As Int()
Dim i As Long
Dim c As Long
Dim n As Long
Dim B4XOM As B4XOrderedMap
Dim lstKeys As List
If Check1DStringAllSame(arrString, bCaseInsensitive) Then
Dim arrIndex(1) As Int
arrIndex(0) = -1
Return arrIndex
End If
B4XOM.Initialize
For i = 0 To arrString.Length - 1
Dim lstIndexes As List
If B4XOM.ContainsKey(arrString(i)) Then
lstIndexes = B4XOM.Get(arrString(i))
Else
lstIndexes.Initialize
End If
lstIndexes.Add(i)
B4XOM.Put(arrString(i), lstIndexes)
Next
If bCaseInsensitive Then
B4XOM.Keys.SortCaseInsensitive(True)
Else
B4XOM.Keys.Sort(True)
End If
Dim arrIndex(arrString.Length) As Int
'we need this as it looks we can't access the items by index
lstKeys = B4XOM.Keys
For i = 0 To B4XOM.Size - 1
lstIndexes = B4XOM.Get(lstKeys.Get(i))
For c = 0 To lstIndexes.Size - 1
arrIndex(n) = lstIndexes.Get(c)
n = n + 1
Next
Next
Return arrIndex
End Sub
Sub Check1DStringAllSame(arrstring() As String, bCaseInsensitive As Boolean) As Boolean
Dim i As Int
Dim str As String
If arrstring.Length = 1 Then
Return True
End If
If bCaseInsensitive Then
str = arrstring(0).ToLowerCase
For i = 1 To arrstring.Length - 1
If arrstring(i).ToLowerCase.CompareTo(str) <> 0 Then
Return False
End If
Next
Else
str = arrstring(0)
For i = 1 To arrstring.Length - 1
If arrstring(i).CompareTo(str) <> 0 Then
Return False
End If
Next
End If
Return True
End Sub
Would it be possible to add this method to the B4XOrderedMap?
RBS