I am trying to sort custom objects on some custom order but I am not able to. I can sort if objects are String or integer. I have posted some detailed description on code below. Thanks for any help.
Private static final List<String> places = Arrays.asList(“Switzerland”, “America”, “Romania”, “Chad”, "Australia");
//this list is fixed and always needs to maintain this order
Map<String, String> countrFromDB = countryDAO.getAllCOuntriesFromDB();
List<Country> sortCountry= new ArrayList<>();
for(Map.Entry<String, String> entry : countrFromDB.entrySet() ){
Country c = new Country(entry.getKey(), entry.getValue());
sortCountry.add(c);
if(places.contains(countrFromDB.getKeyValue())){
sortCountry.add(c.getKeyValue());
}
}
for(Country data:sortCountry){
System.out.println(data.getKeyValue());
}
I get America, Chad, Australia, Switzerland, Romania
. However, I need to maintain order like in
places = Switzerland, America, Romania, Chad, Australia