Via Cà Matta 2 - Peschiera Borromeo (MI)
+39 02 00704272
info@synaptica.info

Sorting a Java Map

Digital Innovation Partner

Sorting a Java Map

Hi,
a short post to show a simple tip useful when dealing with Java Maps.
The following Function sorts a Map through a custom Comparator and returns a new ordered LinkedHashMap:

And this is an example of Comparator for MyClass objects sorted on the base of a String property “sortField” (note the toLowerCase method):

The sorting Function is general and is possible to implement a custom Comparator depending on the specific needs.

Ciao
Fede

Tags: , , , ,

Una risposta.

  1. Riccardo ha detto:

    hi,

    why don’t you use the String.compareTo(String) method?
    Has “compareStringFirstChar” better performance?

Lascia un commento