Guest User

Untitled

a guest
Jan 16th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.43 KB | None | 0 0
  1. SharedPreferences sharedPrefs = getSharedPreferences("FileName", MODE_PRIVATE);
  2. Map<String, ?> sharedPrefsMap = sharedPrefs.getAll();
  3.  
  4.  
  5. List<Pair<Object, String>> sortedByValue = new LinkedList<Pair<Object,String>>();
  6. for (Map.Entry<String, ?> entry : sharedPrefsMap.entrySet()) {
  7. Pair<Object, String> e = new Pair<Object, String>(entry.getValue(), entry.getKey());
  8. sortedByValue.add(e);
  9. }
  10.  
  11. // Pair doesn't have a comparator, so you're going to need to write one.
  12. Collections.sort(sortedByValue, new Comparator<Pair<Object, String>>() {
  13. public int compare(Pair<Object, String> lhs, Pair<Object, String> rhs) {
  14. // This is a naive and shitty comparator, but it works for
  15. // arbitrary objects. Sort of. Tweak depending on the order you need.
  16. String sls = String.valueOf(lhs.first);
  17. String srs = String.valueOf(rhs.first);
  18. int res = sls.compareTo(srs);
  19. // Sort on value first, key second
  20. return res == 0 ? lhs.second.compareTo(rhs.second) : res;
  21. }
  22. });
  23.  
  24. for (Pair<Object, String> pair : sortedByValue) {
  25. Log.i("map values", pair.first + "/" + pair.second);
  26. }
  27.  
  28. Collection<?> stringArrayList = sharedPrefsMap.values();
  29. CharSequence[] prefsCharSequence = stringArrayList.toArray(new CharSequence[stringArrayList.size()]);
Add Comment
Please, Sign In to add comment