问题
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...)
回答1:
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>
回答2:
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]
回答3:
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.
回答4:
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.
来源:https://stackoverflow.com/questions/20566921/sort-an-arraylist-with-a-specific-order