How to sort an ArrayList?

后端 未结 20 2055
有刺的猬
有刺的猬 2020-11-22 06:19

I have a List of doubles in java and I want to sort ArrayList in descending order.

Input ArrayList is as below:

List testList = new Arr         


        
相关标签:
20条回答
  • 2020-11-22 06:34

    In JAVA 8 its much easy now.

    List<String> alphaNumbers = Arrays.asList("one", "two", "three", "four");
    List<String> alphaNumbersUpperCase = alphaNumbers.stream()
        .map(String::toUpperCase)
        .sorted()
        .collect(Collectors.toList());
    System.out.println(alphaNumbersUpperCase); // [FOUR, ONE, THREE, TWO]
    

    -- For reverse use this

    .sorted(Comparator.reverseOrder())
    
    0 讨论(0)
  • 2020-11-22 06:35

    If you have to sort object based on its id in the ArrayList , then use java8 stream.

     List<Person> personList = new ArrayList<>();
    
        List<Person> personListSorted =
                    personList.stream()
                      .sorted(Comparator.comparing(Person::getPersonId))
                      .collect(Collectors.toList());
    
    0 讨论(0)
  • 2020-11-22 06:37

    Descending:

    Collections.sort(mArrayList, new Comparator<CustomData>() {
        @Override
        public int compare(CustomData lhs, CustomData rhs) {
            // -1 - less than, 1 - greater than, 0 - equal, all inversed for descending
            return lhs.customInt > rhs.customInt ? -1 : (lhs.customInt < rhs.customInt) ? 1 : 0;
        }
    });
    
    0 讨论(0)
  • 2020-11-22 06:37

    You can do like this:

    List<String> yourList = new ArrayList<String>();
    Collections.sort(yourList, Collections.reverseOrder());
    

    Collection has a default Comparator that can help you with that.

    Also, if you want to use some Java 8 new features, you can do like that:

    List<String> yourList = new ArrayList<String>();
    yourList = yourList.stream().sorted(Collections.reverseOrder()).collect(Collectors.toList());
    
    0 讨论(0)
  • 2020-11-22 06:39

    if you are using Java SE 8, then this might be of help.

    //create a comparator object using a Lambda expression
    Comparator<Double> compareDouble = (d1, d2) -> d1.compareTo(d2);
    
    //Sort the Collection in this case 'testList' in reverse order
    Collections.sort(testList, Collections.reverseOrder(compareDouble));
    
    //print the sorted list using method reference only applicable in SE 8
    testList.forEach(System.out::println);
    
    0 讨论(0)
  • 2020-11-22 06:40

    For your example, this will do the magic in Java 8

    List<Double> testList = new ArrayList();
    testList.sort(Comparator.naturalOrder());
    

    But if you want to sort by some of the fields of the object you are sorting, you can do it easily by:

    testList.sort(Comparator.comparing(ClassName::getFieldName));
    

    or

     testList.sort(Comparator.comparing(ClassName::getFieldName).reversed());
    

    or

     testList.stream().sorted(Comparator.comparing(ClassName::getFieldName).reversed()).collect(Collectors.toList());
    

    Sources: https://docs.oracle.com/javase/8/docs/api/java/util/Comparator.html

    0 讨论(0)
提交回复
热议问题