Hi,
I am trying to work out how to sort a map.
I have it working fine with B4A but now using the same code on B4i and it's not working.
It seems to not sort it correctly on B4i. On B4A it sorts it fine. (using the same data)
The code I have used is:
I am sorting the Map with the following code (which works fine in B4A)
I am then viewing the results:
When I view the B4i log it shows:
In B4A it shows (which is correct, so I guess my code should of worked):
Any ideas on what I have done wrong?
I am wanting to sort the Ticks (time and date) which is stored in the Map as the Key, and then going to display the value based on the order the Ticks (time and date) is in.
If there is a better way, then can you provide an example as I have working on this for many days, and can't work out a better way in doing this.
I am trying to work out how to sort a map.
I have it working fine with B4A but now using the same code on B4i and it's not working.
It seems to not sort it correctly on B4i. On B4A it sorts it fine. (using the same data)
The code I have used is:
B4X:
Dim EventList as Map
EventList.Initialize
EventList.Put("1545915780827","ABC1")
EventList.Put("1544739267005","ABC2")
EventList.Put("1545869166546","ABC3")
EventList.Put("1545869223365","ABC4")
EventList.Put("1545869166766","ABC5")
EventList.Put("1545869166582","ABC6")
EventList.Put("1545870879803","ABC7")
EventList.Put("1544739267025","ABC8")
EventList.Put("1545870884867","ABC9")
' this will sort the map
SortMapKeys(EventList,False)
I am sorting the Map with the following code (which works fine in B4A)
B4X:
Sub SortMapKeys (m As Map, SortAsc As Boolean)
Private KeysList As List:KeysList.Initialize
Private m2 As Map:m2.Initialize
For Each item As String In m.keys
KeysList.Add(item)
Next
KeysList.Sort(SortAsc)
For x=0 To KeysList.Size - 1
Private key As String = KeysList.Get(x)
Private val As String = m.Get(key)
m2.Put(key, val)
Next
m.Clear
For Each m2Key As String In m2.Keys
m.Put(m2Key, m2.Get(m2Key))
Next
End Sub
I am then viewing the results:
B4X:
DateTime.DateFormat = "EEEE d MMMM yyyy h:mma"
For Each key As String In EventList.Keys
Log(key & " " & DateTime.Date(key))
Next
When I view the B4i log it shows:
B4X:
1545869223365 Thursday 27 December 2018 11:07am
1545870884867 Thursday 27 December 2018 11:34am
1544739267025 Friday 14 December 2018 9:14am
1545870879803 Thursday 27 December 2018 11:34am
1545869166546 Thursday 27 December 2018 11:06am
1545869166582 Thursday 27 December 2018 11:06am
1545869166766 Thursday 27 December 2018 11:06am
1545915780827 Friday 28 December 2018 12:03am
1544739267005 Friday 14 December 2018 9:14am
In B4A it shows (which is correct, so I guess my code should of worked):
B4X:
1545915780827 Friday 28 December 2018 12:03am
1545870884867 Thursday 27 December 2018 11:34am
1545870879803 Thursday 27 December 2018 11:34am
1545869223365 Thursday 27 December 2018 11:07am
1545869166766 Thursday 27 December 2018 11:06am
1545869166582 Thursday 27 December 2018 11:06am
1545869166546 Thursday 27 December 2018 11:06am
1544739267025 Friday 14 December 2018 9:14am
1544739267005 Friday 14 December 2018 9:14am
Any ideas on what I have done wrong?
I am wanting to sort the Ticks (time and date) which is stored in the Map as the Key, and then going to display the value based on the order the Ticks (time and date) is in.
If there is a better way, then can you provide an example as I have working on this for many days, and can't work out a better way in doing this.