About 383,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. How can I sort an ArrayList of Strings in Java? - Stack Overflow

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

  4. 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, …

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

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

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

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

  9. How to use Comparator in Java to sort - Stack Overflow

    Use People implements Comparable<People> instead; this defines the natural ordering for People.. A Comparator<People> can also be defined in addition, but People implements Comparator<People> is not the right way of doing things.

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

Refresh