I have a list of objects and I would like to sort it with a defined order. For ex. I have an object with a field String color
. I would like to sort my list on the color field so that it always has first white than blue than yellow and than all the others(if possible alph. ordered but not necessary):
Before sorting: After sorting:
orange white
white blue
green yellow
brown orange
yellow black
black brown
... ...
Is there a (easy) way to do that?
EDIT:
I have to add a complication more...
What if there can be more colors with the same name/radix? For ex. whiteX, whiteY, whiteZ, blueA, blueB, ...
All the whites must come first than all the blues than all the yellows and than all the others. It is still possible to solve that with a comparator? (I can't imagine how...)
here is an other option :
Map<Integer, String> tree = new TreeMap<Integer, String>();
List<String> listOrdre = Arrays.asList("white", "blue", "yellow", "orange", "black", "brown");
List<String> myList = Arrays.asList("orange", "white", "brown", "yellow", "black");
for (String code : myList) {
tree.put(listOrdre.indexOf(code), code);
}
System.out.println(tree.values()); // -> [white, yellow, orange, black, brown]
Yes you can create a Comparator for creating your sort strategy, or define natural-order of your class implementing Comparable
As a side note :
It is strongly recommended, but not strictly required that (x.compareTo(y)==0) == (x.equals(y))
Example using Comparator:
class MyClass {
private Color color;
private String someOtherProperty;
public static final Comparator<MyClass> COLOR_COMPARATOR = new MyComparator();
//getter and setter
static class MyComparator implements Comparator<MyClass>{
@Override
public int compare(MyClass o1, MyClass o2) {
// here you do your business logic, when you say where a color is greater than other
}
}
}
And in client code.
Example:
List<MyClass> list = new ArrayList<>();
//fill array with values
Collections.sort(list, MyClass.COLOR_COMPARATOR );
Read more : Collections#sort(..)
If you want to define natural-ordering of your class just define
public class MyClass implements Comparable<MyClass>{
@Override
public int compareTo(MyClass o) {
// do business logic here
}
}
And in client code:
Collections.sort(myList); // where myList is List<MyClass>
you can use comparator
.
another thing you can do is set some values ( say 1 to n ) to the numbers. For example in your case give give white 1, give blue 2, give yellow 3. now sort those numbers.
Another solution is to use enums with your comparator since enums have an index already defined ( ordinal ).
First, create an enum with your values in the order you want it to be sorted to.
enum class MyColour {
WHITE,
BLUE,
YELLOW,
ORANGE,
BLACK,
BROWN
}
For each object, you can get the enum value with Mycolour.valueOf("WHITE")
. Note: this is case sensitive.
Next, you can simply use your comparator.
val sortedList = list.sortedBy { it.colour } // colour being the enum value we defined.
Thanks for Réda's answer.
// Weight is the index in colorOrder, or -1 for the others
List<String> colorOrder = Arrays.asList("yellow", "blue", "white");
List<String> myList = Arrays.asList("orange", "white", "brown", "yellow", "black");
Collections.sort(myList, Comparator.comparingInt(colorOrder::indexOf).reversed());
System.out.println(myList);//[white, yellow, orange, brown, black]
And this is easy to chain some other Comparator, like alph. order.
Collections.sort(myList, Comparator.comparingInt(colorOrder::indexOf).reversed().thenComparing(Object::toString));
System.out.println(myList);//[white, yellow, black, brown, orange]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.