String
s are comparable in a lexicographic order (i.e., the order they’d appear in a dictionary). If you want to implement a Comparator
for clothes sizes, you’d have to write the logic yourself. E.g.:
public class ClothesSizeComparator implements Comparator<String> {
private static final List<String> SIZES =
Arrays.asList("XS", "S", "M", "L", "XL", "XXL", "3XL");
@Override
public int compare(String s1, String s2) {
int s1pos = SIZES.indexOf(s1);
int s2pos = SIZES.indexOf(s2);
return Integer.compare(s1pos, s2pos);
}
}
Note: This Comparator
assumes that both strings represent valid clothe sizes. If this assumption can’t be made, you’d have to add some error handling.
1
solved How “java.util.Comparator.compare(String o1, String o2) ” method works