Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sort SparseArray in Android

Tags:

android

How can I sort android.util.SparseArray? E.g. I have SparseArray with:
1 - 2.33
5 - 1.5
Result:
5 - 1.5
1 - 2.33

Thanks!!!

-- EDITED

I've used the Map. Thanks for help.

like image 234
rocknow Avatar asked Aug 28 '12 10:08

rocknow


2 Answers

It is not clear if you requested a key or value sorted order ...

so just a note:

A binary search only works on sorted data and SparseArray uses a binary search for its sorted (!) keys array according to the source. So the keys ARE already sorted and won't accept a different order like the order from the values.

like image 140
Karussell Avatar answered Sep 29 '22 19:09

Karussell


One small amendment to @Karussell's spot-on answer is that also the documentation itself of the valueAt() method suggests a sorted order (on keys):

[...] valueAt(0) will return the value associated with the smallest key and valueAt(size()-1) will return the value associated with the largest key.

A similar description is given for the keyAt() method.

like image 42
dbm Avatar answered Sep 29 '22 18:09

dbm