简体   繁体   中英

Sort key-value pair of a Bundle in android

I have a list of key-value pairs in a Bundle

params.keySet()

which needs to be sorted alphabetically on the key column.

I tried the long way of extracting the key-value pairs and sorting them using

Collections.sort(list)

and putting it back inside a new Bundle.

But when I call params.keySet(). They come to un-sorted again. Any ideas. Is their an easy way?

Bundle is backed up by a HashMap , so it does not guarantee the order of keys. You'll have to reconsider the logic in your app to break the dependency on the order of the keys in the Bundle.

public Set<String> keySet () Set only guarantees no duplicates but not order.

For your requirement, you can use some subclass of java.util.SortedMap instead of Bundle .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM