Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sort an (Array)List with a specific order

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...)

like image 913
Francesco Avatar asked Dec 13 '13 12:12

Francesco


People also ask

How do you sort a specific element in an ArrayList?

In order to sort elements in an ArrayList in Java, we use the Collections. sort() method in Java. This method sorts the elements available in the particular list of the Collection class in ascending order. where list is an object on which sorting needs to be performed.

How do you sort an ArrayList of strings?

An ArrayList can be sorted by using the sort() method of the Collections class in Java. It accepts an object of ArrayList as a parameter to be sort and returns an ArrayList sorted in the ascending order according to the natural ordering of its elements.

Can you sort a list of lists Java?

We can sort a list in lexicographical order using a custom comparator. The following code implements a custom comparator and passes it to List's sort() method.

How do you sort an ArrayList in Java 8?

There are multiple ways to sort a list in Java 8, for example, you can get a stream from the List and then use the sorted() method of Stream class to sort a list like ArrayList, LinkedList, or Vector and then convert back it to List. Alternatively, you can use the Collections. sort() method to sort the list.


2 Answers

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]
like image 166
Réda Avatar answered Sep 22 '22 15:09

Réda


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.
like image 44
Advice-Dog Avatar answered Sep 22 '22 15:09

Advice-Dog