le tri personnalisé de l'objet personnalisé

user10806781:

Je suis en train de trier des objets personnalisés sur un ordre personnalisé mais je ne suis pas capable. Je peux trier les objets sont si chaîne ou entier. J'ai posté une description détaillée sur le code ci-dessous. Merci pour toute aide.

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());
}

Je reçois America, Chad, Australia, Switzerland, Romania. Cependant, je dois maintenir l' ordre comme dans

places = Switzerland, America, Romania, Chad, Australia
Dickens:

Vous devez utiliser indexOfdans le comparateur

final List<String> places = Arrays.asList("Switzerland", "America", "Romania", "Chad", "Australia"); 
.....
.....
.....

Collections.sort(sortCountry, new Comparator<Country>(){
    public int compare(Country o1, Country o2){
        return places.indexOf(o1.getValue()) - places.indexOf(o2.getValue());
    }
});

for(Country data:sortCountry){
    System.out.println(data.getValue());
}

Je suppose que tu aimes

Origine http://43.154.161.224:23101/article/api/json?id=373336&siteId=1
conseillé
Classement