-1

I am using ArrayMap in my Activity for storing my response. ArrayMap<String, PublicSpotData> publicSpotData ArrayMap will store index as well as key-value pair. Now I have problem in sorting ArrayMap. I want to sort ArrayMap according to publicSpotData.getAt() (int). I have already done this.

List<Map.Entry<String, PublicSpotData>> entries = new ArrayList<>(); for (Map.Entry<String, PublicSpotData> entry : publicSpotData.entrySet()) { entries.add(entry); }

 Collections.sort(entries, new Comparator<Map.Entry<String, PublicSpotData>>() {
                @Override
                public int compare(Map.Entry<String, PublicSpotData> o1, Map.Entry<String, PublicSpotData> o2) {
                    return o2.getValue().getAt().compareTo(o1.getValue().getAt());
                }
            });

            for (Map.Entry<String, PublicSpotData> entry : entries)
                publicSpotData.put(entry.getKey(), entry.getValue());`

But this not solved my issue. Any help would be appreciated.

Mubashar Javed
  • 749
  • 1
  • 5
  • 12

3 Answers3

1

you can use Collections#sort method

Collections.sort(new ArrayList<>(c), aComparator);

c is just a key set from the ArrayMap and aComparator is a custom implemetation of comparator to sort the elemets in the map

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
1

You can sort any Java collection by providing a Comparator.

In other words: you have to create a class that knows how to compare two PublicSpotData objects; and then you use that with Collections.sort().

Meaning: as long as your "incoming" object implements one of the Java collection interfaces (and the Android ArrayMap is a Map, which is a collection); you can sort it using the default means of the java library.

But then, the problem is more complicated: you can only sort Lists. And your input data ... is represented as Map. So, lets go step by step:

List<PublicSpotData> dataObjects = ... coming from somewhere
Collections.sort(dataObjects, new Comparator<PublicSpotData>() { ...
  here you put the comparator for that thing);

Now you got a list that is sorted for that criteria.

In that sense: the real problem is that your model doesn't support your needs. The essence of a Map is to provide that mapping functionality. You need to:

  • extract a list of data objects
  • sort those
  • iterate the sorted list; and for each entry ... find the corresponding map key!
GhostCat
  • 137,827
  • 25
  • 176
  • 248
0

Firstly ArrayMap implements sorting implicitly for keys.

Baseline: You need to iterate the arrayMap to obtain Key by index. then call value by that key.

Here is an example of that. I list sorted outputs from unsorted inputs. then I sort the sort the keySet reversely.

ArrayMap<String, String> arrayMap = new ArrayMap<>();
        arrayMap.put("AAA7", "BBB2");
        arrayMap.put("AAA8", "BBB1");
        arrayMap.put("AAA9", "BBB");
        arrayMap.put("AAA9", "BBB");// will override previous call

        String res = "";
        for (int i = 0; i < arrayMap.size(); i++) {
            String key = arrayMap.keyAt(i);
            res += arrayMap.get(key) + ", ";
        }

        Log.d(TAG, "normal output: " + res);

        // sorting array based on keys
        List<String> list = new ArrayList<>(arrayMap.keySet());
        java.util.Collections.reverse(list);//or you implement your own sort

        String res2 = "";
        for (String key : list) {
            res2 += arrayMap.get(key) + ", ";
        }

        Log.d(TAG, "output of sorted collection in reverse based on keys: " + res2);

For more efficiency, you might use SimpleArrayMap if you don't need standard Java API like Iteration.

Maher Abuthraa
  • 17,493
  • 11
  • 81
  • 103