Sub TestB4XOrderedMapValuesSort
Dim i As Int
Dim OM As B4XOrderedMap
Dim OMSortedOnIntValues As B4XOrderedMap
Dim lstValuesUnSorted As List
Dim lstValuesSorted As List
Dim lstValuesSorted2 As List
OM.Initialize
OM.Put("Key0", 10)
OM.Put("Key1", 2)
OM.Put("Key2", 6)
lstValuesUnSorted = OM.Values
For i = 0 To 2
Log("Before OM.Values.Sort: lstValuesUnSorted.Get(" & i & ")" & lstValuesUnSorted.Get(i))
Next
Log("OM.Values.Sort(True)")
OM.Values.Sort(True)
lstValuesSorted = OM.Values
For i = 0 To 2
Log("After OM.Values.Sort: lstValuesSorted.Get(" & i & ")" & lstValuesSorted.Get(i))
Next
Log("SortB4XOMByIntValues(OM, True)")
OMSortedOnIntValues = SortB4XOMByIntValues(OM,True)
lstValuesSorted2 = OMSortedOnIntValues.Values
For i = 0 To 2
Log("After SortB4XOMByIntValues: " & OMSortedOnIntValues.Keys.Get(i) & ":, lstValuesSorted2.Get(" & i & ")" & lstValuesSorted2.Get(i))
Next
End Sub
Sub SortB4XOMByIntValues(oB4XOM As B4XOrderedMap, bAscending As Boolean) As B4XOrderedMap
Dim i As Int
Dim arrKeys(oB4XOM.Size) As String
Dim arrValues(oB4XOM.Size) As Int
Dim arrIndex() As Int
Dim oB4XOMSorted As B4XOrderedMap
For Each strKey As String In oB4XOM.Keys
arrKeys(i) = strKey
arrValues(i) = oB4XOM.Get(strKey)
i = i + 1
Next
arrIndex = SortOMIntIDX(arrValues, bAscending, -1, -1)
If arrIndex(0) = -1 Then
'all values same, so return old B4XOrderedMap
Return oB4XOM
End If
oB4XOMSorted.Initialize
For i = 0 To arrIndex.Length - 1
oB4XOMSorted.Put(arrKeys(arrIndex(i)), arrValues(arrIndex(i)))
Next
Return oB4XOMSorted
End Sub
Sub Check1DIntAllSame(arrInt() As Int, iStart As Int, iEnd As Int) As Boolean
Dim i As Int
Dim iVal As Int
If arrInt.Length = 1 Then
Return True
End If
If iStart = -1 Then
iStart = 1
End If
If iEnd = -1 Then
iEnd = arrInt.Length - 1
End If
iVal = arrInt(0)
For i = iStart To iEnd
If arrInt(i) <> iVal Then
Return False
End If
Next
Return True
End Sub
Sub SortOMIntIDX(arrInt() As Int, bAscending As Boolean, iStart As Int, iEnd As Int) As Int()
Dim i As Long
Dim c As Long
Dim n As Long
Dim B4XOM As B4XOrderedMap
If Check1DIntAllSame(arrInt, iStart, iEnd) Then
Dim arrIndex2(1) As Int
arrIndex2(0) = -1
Return arrIndex2
End If
B4XOM.Initialize
If iStart = -1 Then
iStart = 0
End If
If iEnd = -1 Then
iEnd = arrInt.Length - 1
End If
For i = iStart To iEnd
Dim lstIndexes As List
If B4XOM.ContainsKey(arrInt(i)) Then
lstIndexes = B4XOM.Get(arrInt(i))
Else
lstIndexes.Initialize
End If
lstIndexes.Add(i)
B4XOM.Put(arrInt(i), lstIndexes)
Next
B4XOM.Keys.Sort(bAscending)
Dim arrIndex((iEnd - iStart) + 1) As Int
For Each oValue As Object In B4XOM.Values
lstIndexes = oValue
For c = 0 To lstIndexes.Size - 1
arrIndex(n) = lstIndexes.Get(c)
n = n + 1
Next
Next
Return arrIndex
End Sub