Bonjour,
Je souhaites trier une ArrayList selon deux critères :
J'ai une ArrayList qui contient des objets "produit". Un objet "produit" contient en autre un attribut "magasin" et un attribut "description".
Je voudrais classer ma liste en ordre alphabétique par magasin puis par description. En fait, une liste de ce style :
<magasin1,description1><magasin1,description2><magasin1,description3><magasin2,description1><magasin2,description2><magasin2,description3><magasin3,description1><...>
En utilisant un Collections.sort(maListe) ce la fonctionne mais que sur un seul critère.
J'ai utilisé l'aide : http://java.developpez.com/faq/java/...rierCollection de cette façon :
qui les classe par "magasin"
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12 public int compareTo(Object other) { String m1 = ((Produit) other).getMagasin(); String m2 = this.getMagasin(); if (m1.compareTo(m2) > 0) { return -1; } else if (m1.equals(m2)) { return 0; } else { return 1; } }
Auriez vous une idée ?
merci
Partager