 # How to sort TreeMap in reverse order ?

By default, TreeMap stores the entries in ascending logic, from the smallest key to the biggest. Take a look on this sample:

```@Test
public void normalTreeMap() {
Map<String, String> alphabet = new TreeMap<String, String>();
alphabet.put("C", "letter C");
alphabet.put("Z", "letter Z");
alphabet.put("A", "letter A");

String[] expected = new String[] {"A", "C", "Z"};
String[] keys = alphabet.keySet().toArray(new String[alphabet.keySet().size()]);
for (int i = 0; i < keys.length; i++) {
assertTrue("TreeMap's key "+keys[i]+" should be the same as expected "+expected[i], keys[i].equals(expected[i]));
}
}
```

We can also demand to return the result in reverse order, from the biggest key to the smallest. In our case, it will look like:

```@Test
public void reverseTreeMap() {
Map<String, String> alphabet = new TreeMap<String, String>(Collections.reverseOrder());
alphabet.put("C", "letter C");
alphabet.put("Z", "letter Z");
alphabet.put("A", "letter A");

String[] expected = new String[] {"Z", "C", "A"};
String[] keys = alphabet.keySet().toArray(new String[alphabet.keySet().size()]);
for (int i = 0; i < keys.length; i++) {
assertTrue("TreeMap's key "+keys[i]+" should be the same as expected "+expected[i], keys[i].equals(expected[i]));
}
}
```

The only difference is the use of Collections.reverseOrder method. We can pass it an instance of the java.util.Comparator<T> implementation to make customized reverse ordering.