Wish B4XCache - make keys Object rather than String

Chris2

Active Member
Licensed User
Longtime User
Currently in the B4XCache class, the cache keys are decalred as Strings. This means that numbers don't sort properly when using B4XCache.Keys.Sort.
Could the keys be changed to be decalred as Object as they already are in B4XOrderedMap?

Example:
B4X:
    Private cache As B4XCache
    Private b4xmap As B4XOrderedMap
    b4xmap.Initialize
    cache.Initialize
   
    For Each i As Int In Array As Int(1, 362, 5, 99, 1234, 0)
        cache.put(i, "")
        b4xmap.Put(i, "")
    Next
   
    Log(cache.Keys)
    cache.Keys.Sort(True)
    Log(cache.Keys)
   
    Log(b4xmap.Keys)
    b4xmap.keys.Sort(True)
    Log(b4xmap.Keys)

Ouputs:
(ArrayList) [1, 362, 5, 99, 1234, 0]
(ArrayList) [0, 1, 1234, 362, 5, 99]
(ArrayList) [1, 362, 5, 99, 1234, 0]
(ArrayList) [0, 1, 5, 99, 362, 1234]
 

William Lancee

Well-Known Member
Licensed User
Longtime User
The B4XCache.bas is a small class that wraps the B4XOrderedMap object.

It is easy to make a copy, rename it and make the modification you want. That's the beauty of an open-source B4XLib
I did a find and replace, but I did not test it - it might not work! See attached.
 

Attachments

  • B4XCacheX.bas
    3.1 KB · Views: 141

Chris2

Active Member
Licensed User
Longtime User
Thanks @William Lancee.
I needed to rename the B4XCacheItem custom type in the class as well in order to avoid a conflict with the original. After that it seems to work as I hoped.
 

Attachments

  • B4XCache2.bas
    3.4 KB · Views: 144
Top