
How to use Comparator in Java to sort - Stack Overflow
If you are using Java 8 then it's better to use below code like this: Comparator<People> comparator = Comparator.comparing(People::getName); And then simply use: Collections.sort(list, comparator); If you are using Java 7 or below then you can use a comparator for customized sorting order by implementing compare method. For example:
Java Comparator class to sort arrays - Stack Overflow
java.nio.IntBuffer#wrap(int[]) provides an excellent built-in way to compare two instances of int[], since IntBuffer is both a lightweight wrapper for int[] instances, and implements Comparable. Using it combined with other built-in Comparator features has several advantages over the examples in other answers I see here: Compares all sub-array ...
How to sort an array of ints using a custom comparator?
Sep 13, 2010 · private int[] sortDescending(int[] source) { Integer[] intObjArr = ArrayUtils.toObject(source); Arrays.sort(intObjArr, Comparator.reverseOrder()); return ArrayUtils.toPrimitive(intObjArr); } Version 2: Using Arrays.stream() and available functions to box the int values, sort and, map them back to primitive type array.
How do I use Comparator to define a custom sort order?
Mar 9, 2011 · I am using data table to display car list. Now actually I want to sort the list by car color. Here it is not sort by alphabetic order. I want to use my custom sorting order like Red car come first, then Blue, etc. For that I try to use Java Comparator and Comparable but it allows to sort in alphabetic order only.
java - Collections.sort() using comparator? - Stack Overflow
Dec 16, 2014 · Collection.sort(1, Comparator) uses a custom comparator to compare the contents of l, this is what you did. The very idea of sorting (including the sort() method) implies the objects MUST be comparable - in this case, with either Comparable or Comparator. Note that many Java objects are comparable already, including String, Date and Number.
java - sort List of string using comparator - Stack Overflow
Aug 27, 2012 · sort List of string using comparator. Ask Question Asked 12 ... Sorting on List using Comparator in Java. 1.
java - Sort ArrayList of custom Objects by property - Stack Overflow
May 7, 2010 · Using Java 8 use can define the Comparator in one line using Comparator.comparing() Use any of the following way: Option 1: listToBeSorted.sort(Comparator.comparing(CustomObject::getStartDate)); Option 2: Collections.sort(listToBeSorted, Comparator.comparing(CustomObject::getStartDate));
java - How to sort ArrayList using Comparator? - Stack Overflow
Mar 11, 2014 · Use the Collections.sort(List, Comparator) method: Collections.sort(students, Student.getCompByName()); Also in your code it would be good to use the List interface when declaring the List: List<Student> students = new ArrayList(); You could also tighten up the code by using a Student[] and passing it to the ArrayList constructor:
java - Sorting using Comparator- Descending order (User defined …
The java.util.Collections class has a sort method that takes a list and a custom Comparator. You can define your own Comparator to sort your Person object however you like. You can define your own Comparator to sort your Person object however you like.
sorting - How to sort by two fields in Java? - Stack Overflow
Mar 22, 2018 · I would instead create a static Comparator using the newest Java 8 API if possible or Guava's Ordering API which allows you to do that, here is an example with Java 8: import java.util.Comparator; import static java.util.Comparator.naturalOrder; import static java.util.Comparator.nullsLast; private static final Comparator<Person> COMPARATOR ...