0

Assume that there is an ArrayList<Object[]> containing various objects inside:

ArrayList<Object[]> list = new ArrayList<>();
list.add(new Object[]{"cat", (long)1422355945});
list.add(new Object[]{"dog", (long)1422355970});
list.add(new Object[]{"rat", (long)1422355900});

How can we sort this list effectively by first Object (which is String) in reverse order? And to do the same by second Object (which is long)?

Ernestas Gruodis
  • 8,567
  • 14
  • 55
  • 117

1 Answers1

1

Just create a Comparator, similar to the example below:

    List<Object[]> list = new ArrayList<>();
    list.add(new Object[] { "cat", (long) 1422355945 });
    list.add(new Object[] { "dog", (long) 1422355970 });
    list.add(new Object[] { "rat", (long) 1422355900 });

    Collections.sort(list, new Comparator<Object[]>() {
        @Override
        public int compare(Object[] o1, Object[] o2) {
            int cmp1 = ((String) o1[0]).compareTo((String) o2[0]);
            if (cmp1 == 0) {
                return Long.compare((Long) o1[1], (Long) o2[1]);
            }
            return cmp1;
        }
    });
Ernestas Gruodis
  • 8,567
  • 14
  • 55
  • 117
nogard
  • 9,432
  • 6
  • 33
  • 53