0

I want to sort a Set<TailleDetail> by one of its string attributes (mesure), but the only solutions I saw in the internet (TreeSet and Comparator) don't work, can you help me?

My class:

public class TailleDetail {
    private Integer id;
    private String sexe;
    private String mesure;
    private Taille Taille;
}

EDIT -

For TreeSet I just try this:

Set<TailleDetail> tailles = new TreeSet<TailleDetail>();

to remplace :

Set<TailleDetail> tailles = new HashSet<TailleDetail>();

And For Comparator I try this :

Set<TailleDetail> tailles = new HashSet<TailleDetail>();
Comparator<TailleDetail> comparatorTaille = new Comparator<TailleDetail>() {
@Override
public int compare(TailleDetail left, TailleDetail right) {
    return left.toString().compareToIgnoreCase(right.toString());
    }
};

List<TailleDetail> tai = tailleDetailViewManager.search(param, true);

Collections.sort(tai, comparatorTaille);

tailles = new HashSet<TailleDetail>(tai);
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
B.Xavier
  • 3
  • 5
  • 1
    Add the code you tried with treeset – stinepike Apr 06 '17 at 14:26
  • Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a [mcve]. – Erwin Bolwidt Apr 06 '17 at 14:32
  • 1
    This is the line that screws everything up: `tailles = new HashSet(tai);` - a HashSet does not have a sorting that you can depend on and it certainly doesn't keep the order of the list tat you used to populate it. Using a LinkedHashSet would work - but it's still a bad idea. Instead, pass a instance of the comparator to the constructor TreeSet constructor. – Erwin Bolwidt Apr 06 '17 at 14:45

2 Answers2

2

The following code should make the trick :


       final Set tailleDetails = new TreeSet(new TailleDetailComparator());
                tailleDetails.add(...);

        public class TailleDetailComparator implements Comparator {

                @Override
                public int compare(final TailleDetail o1, final TailleDetail o2) {
                    return o1.mesure.compareTo(o2.mesure);
                }
            }

Note that comparison is going to be done using the string comparison rules (alphabetically).

Ruben
  • 761
  • 5
  • 9
1

You're comparator does not compare by mesure, but by it's toString() method. You should adapt your comparator to sort by mesure.

You List is already sorted, so why use a set ?

List<TailleDetail> tai = tailleDetailViewManager.search(param, true);
Collections.sort(tai, (td1, td2) -> td1.getMesure().compareTo(td2.getMesure())); // tai will be sorted by mesure

If you do want to use a Set, use a TreeSet:

SortedSet<TailleDetail> set = new TreeSet<>((td1, td2) -> td1.getMesure().compareTo(td2.getMesure())); // also sorted by mesure
set.addAll(list)

Also see When should a class be Comparable and/or Comparator?

Community
  • 1
  • 1
  • Because I work with a code create by another People and this function need a Set, not a List, and the List is not sorted correcly... – B.Xavier Apr 06 '17 at 14:49