About 386,000 results
Open links in new tab
  1. java - How to sort a List/ArrayList? - Stack Overflow

    Apr 27, 2013 · One should actually use Collections.sort(list, Collections.reverseOrder());.Apart from being more idiomatic (and possibly more efficient), using the reverse order comparator makes sure that the sort is stable (meaning that the order of elements will not be changed when they are equal according to the comparator, whereas reversing will change the order).

  2. java - How to sort List of objects by some property - Stack Overflow

    We can sort the list in one of two ways: 1. Using Comparator: When required to use the sort logic in multiple places If you want to use the sorting logic in a single place, then you can write an anonymous inner class as follows, or else extract the comparator and use it in multiple places

  3. Java Sort List of Lists - Stack Overflow

    Mar 3, 2016 · For this example [[1, 3], [1, 2]], if you want to sort the list by both elements you can use sorted from Java 8 manually implemented the comparator method, validating all cases like the following example:

  4. How can I sort an ArrayList of Strings in Java?

    Oct 24, 2012 · Check Collections#sort method. This automatically sorts your list according to natural ordering. You can apply this method on each sublist you obtain using List#subList method. private List<String> teamsName = new ArrayList<String>(); List<String> subList = teamsName.subList(1, teamsName.size()); Collections.sort(subList);

  5. java - How can I sort a List alphabetically? - Stack Overflow

    Apr 2, 2009 · Solution with Collections.sort. If you are forced to use this List, or if your program has a structure like. Create a list; Add some country names; sort them once; never change that list again; then Thilos answer is the best way to do it. Combine it with the advice from Tom Hawtin - tackline and you get

  6. java - How to sort List<Integer>? - Stack Overflow

    Dec 11, 2013 · Collection interface has sort method which is used to sort the elements present in the specified list of Collection in ascending order. This will be the quickest and possibly the best way for your case. Sorting a list in ascending order can be performed as default operation on this way: Collections.sort(list);

  7. Sorting a list with stream.sorted() in Java - Stack Overflow

    Java 8 provides different utility api methods to help us sort the streams better. If your list is a list of Integers(or Double, Long, String etc.,) then you can simply sort the list with default comparators provided by java. List<Integer> integerList = Arrays.asList(1, 4, …

  8. In Java how do you sort one list based on another?

    Jan 25, 2019 · We can use this by creating a list of Integers and sort these using the Collections.sort(). The Collections (Java Doc) class (part of the Java Collection Framework) provides a list of static methods which we can use when working with collections such as list, set and the like. So in a nutshell, we can sort a list by simply calling: java.util ...

  9. sorting - how to sort nested lists in java - Stack Overflow

    Jan 9, 2019 · I want to sort list of subjects within each student and then sorting student objects with roll no.Can it be done with java8 in single line. I am using below code : list.forEach(studentObj -> { studentObj.getSubjects() .sort(Comparator.nullsLast(Comparator.comparing(Subject:: getSubjectCode))); }); then sorting the outside object

  10. java - Sort ArrayList of custom Objects by property - Stack Overflow

    May 7, 2010 · Classes that has a natural sort order (a class Number, as an example) should implement the Comparable interface, whilst classes that has no natural sort order (a class Chair, as an example) should be provided with a Comparator (or an anonymous Comparator class).

Refresh