Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android SharedPreferences putStringSet order/sort

I try to save/restore a set of string and all is working except one thing. When i create my strings i put :

Set<String> set = new HashSet<String>();
for(int i=0; i<toggles.size();i++){
   set.add(toggles.get(i).serialise());                 
}

Order is for example "blutooth" "application" "data". When i get back set :

Set<String> set = prefs.getStringSet(key, new HashSet<String>());
for (String toggle : set){
    Toggle t = new Toggle();
    t.deserialize(toggle);
    toggles.add(t); 
}

I get "application" "bluetooth" "data" they are sort by name and i don't want this. I want to get same order i have save. Anyone can help me ?

like image 668
jaumard Avatar asked Nov 19 '12 01:11

jaumard


2 Answers

This is not possible. Sets are unordered collections.

like image 75
CommonsWare Avatar answered Nov 13 '22 07:11

CommonsWare


You can prefix your strings by numbers, for instance 00application, 01bluetooth, 02data, in the order you want to get them out. Put the Set<String> returned from getStringSet in an Array<Set> and sort it.

Set<String> set = prefs.getStringSet(key, new HashSet<String>());
Array<String> a = set.toArray();
java.util.Arrays.sort(a);
like image 15
Gunther Piez Avatar answered Nov 13 '22 06:11

Gunther Piez