Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- SharedPreferences sharedPrefs = getSharedPreferences("FileName", MODE_PRIVATE);
- Map<String, ?> sharedPrefsMap = sharedPrefs.getAll();
- List<Pair<Object, String>> sortedByValue = new LinkedList<Pair<Object,String>>();
- for (Map.Entry<String, ?> entry : sharedPrefsMap.entrySet()) {
- Pair<Object, String> e = new Pair<Object, String>(entry.getValue(), entry.getKey());
- sortedByValue.add(e);
- }
- // Pair doesn't have a comparator, so you're going to need to write one.
- Collections.sort(sortedByValue, new Comparator<Pair<Object, String>>() {
- public int compare(Pair<Object, String> lhs, Pair<Object, String> rhs) {
- // This is a naive and shitty comparator, but it works for
- // arbitrary objects. Sort of. Tweak depending on the order you need.
- String sls = String.valueOf(lhs.first);
- String srs = String.valueOf(rhs.first);
- int res = sls.compareTo(srs);
- // Sort on value first, key second
- return res == 0 ? lhs.second.compareTo(rhs.second) : res;
- }
- });
- for (Pair<Object, String> pair : sortedByValue) {
- Log.i("map values", pair.first + "/" + pair.second);
- }
- Collection<?> stringArrayList = sharedPrefsMap.values();
- CharSequence[] prefsCharSequence = stringArrayList.toArray(new CharSequence[stringArrayList.size()]);
Add Comment
Please, Sign In to add comment