6

I'm fairly new to Guava API and is trying to sort the keys of a MultiMap in reverse order or descending value. I'm initiating the Map in the following way:

ListMultimap<Date, Map<String, String>> listMultimap = MultimapBuilder.treeKeys().arrayListValues().build();

This sorts the keys in ascending. E.g.:

List multi map iteration: key -->Fri Jan 01 00:00:00 PST 2016   values -->[{test2=testval2}, {test3=testval3}]
List multi map iteration: key -->Sun Jan 01 00:00:00 PST 2017   values -->[{test1=testval1}]
List multi map iteration: key -->Mon Jan 01 00:00:00 PST 2018   values -->[{test0=testval0}]
List multi map iteration: key -->Tue Jan 01 00:00:00 PST 2019   values -->[{test4=testval4}]

I tried creating a custom Date Comparator with TreeMultiMap, but haven't figure a way to do it. This is not syntactically right,but just trying to demonstrate the idea.

static final Comparator<Date> DESC_ORDER = new Comparator<Date>() {
    public int compare(Date e1, Date e2) { 
        return e2.compareTo(e1);
    }
};

SortedSetMultimap<Date, Map<String, String>> treeMultimap = TreeMultimap.create(DESC_ORDER);

Any pointers will be appreciated.

Tagir Valeev
  • 97,161
  • 19
  • 222
  • 334
Shamik
  • 1,671
  • 11
  • 36
  • 64

1 Answers1

9

Try using treeKeys(Comparator):

MultimapBuilder.treeKeys(DESC_ORDER).arrayListValues().build();
Tagir Valeev
  • 97,161
  • 19
  • 222
  • 334