java : sort list of map by multiple key


package homeWork;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class sortSample001 {

public static void main(String[] args) {

List> countries = new ArrayList>();
Map country;
country = new HashMap();
country.put( "name", "japan" );
country.put( "currency", "JPY" );
country.put( "population", 127156000 );
countries.add( country );

country = new HashMap();
country.put( "name", "france" );
country.put( "currency", "EUR" );
country.put( "population", 65073482 );
countries.add( country );

country = new HashMap();
country.put( "name", "japan" );
country.put( "currency", "JPY" );
country.put( "population", 56000 );
countries.add( country );


country = new HashMap();
country.put( "name", "spain" );
country.put( "currency", "EUR" );
country.put( "population", 44904000 );
countries.add( country );

country = new HashMap();
country.put( "name", "russia" );
country.put( "currency", "RUB" );
country.put( "population", 141903979 );
countries.add( country );

for(Mapct:countries) {
System.out.println(ct.get("name")+":"+ct.get("population"));
}
System.out.println("-----------------------");

Comparator > c1 =Comparator.comparing (m -> (String) m.get("name"));
Comparator > c2 =Comparator.comparing (m -> (Integer) m.get("population"));
Comparator > c = c1.thenComparing(c2);
countries.sort(c);

for(Mapct:countries) {
System.out.println(ct.get("name")+":"+ct.get("population"));
}
}
}

result

japan:127156000
france:65073482
japan:56000
spain:44904000
russia:141903979

                                            • -

france:65073482
japan:56000
japan:127156000
russia:141903979
spain:44904000