How to sort an ArrayList?

后端 未结 20 2056
有刺的猬
有刺的猬 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:41

    Here is a short cheatsheet that covers typical cases:

    // sort
    list.sort(naturalOrder())
    
    // sort (reversed)
    list.sort(reverseOrder())
    
    // sort by field
    list.sort(comparing(Type::getField))
    
    // sort by field (reversed)
    list.sort(comparing(Type::getField).reversed())
    
    // sort by int field
    list.sort(comparingInt(Type::getIntField))
    
    // sort by double field (reversed)
    list.sort(comparingDouble(Type::getDoubleField).reversed())
    
    // sort by nullable field (nulls last)
    list.sort(comparing(Type::getNullableField, nullsLast(naturalOrder())))
    
    // two-level sort
    list.sort(comparing(Type::getField1).thenComparing(Type::getField2))
    
    0 讨论(0)
  • 2020-11-22 06:41

    With Eclipse Collections you could create a primitive double list, sort it and then reverse it to put it in descending order. This approach would avoid boxing the doubles.

    MutableDoubleList doubleList =
        DoubleLists.mutable.with(
            0.5, 0.2, 0.9, 0.1, 0.1, 0.1, 0.54, 0.71,
            0.71, 0.71, 0.92, 0.12, 0.65, 0.34, 0.62)
            .sortThis().reverseThis();
    doubleList.each(System.out::println);
    

    If you want a List<Double>, then the following would work.

    List<Double> objectList =
        Lists.mutable.with(
            0.5, 0.2, 0.9, 0.1, 0.1, 0.1, 0.54, 0.71,
            0.71, 0.71, 0.92, 0.12, 0.65, 0.34, 0.62)
            .sortThis(Collections.reverseOrder());
    objectList.forEach(System.out::println);
    

    If you want to keep the type as ArrayList<Double>, you can initialize and sort the list using the ArrayListIterate utility class as follows:

    ArrayList<Double> arrayList =
        ArrayListIterate.sortThis(
                new ArrayList<>(objectList), Collections.reverseOrder());
    arrayList.forEach(System.out::println);
    

    Note: I am a committer for Eclipse Collections.

    0 讨论(0)
  • 2020-11-22 06:43

    For example I have a class Person: String name, int age ==>Constructor new Person(name,age)

    import java.util.Collections;
    import java.util.ArrayList;
    import java.util.Arrays;
    
    
    public void main(String[] args){
        Person ibrahima=new Person("Timera",40);
        Person toto=new Person("Toto",35);
        Person alex=new Person("Alex",50);
        ArrayList<Person> myList=new ArrayList<Person>
        Collections.sort(myList, new Comparator<Person>() {
            @Override
            public int compare(Person p1, Person p2) {
                // return p1.age+"".compareTo(p2.age+""); //sort by age
                return p1.name.compareTo(p2.name); // if you want to short by name
            }
        });
        System.out.println(myList.toString());
        //[Person [name=Alex, age=50], Person [name=Timera, age=40], Person [name=Toto, age=35]]
        Collections.reverse(myList);
        System.out.println(myList.toString());
        //[Person [name=Toto, age=35], Person [name=Timera, age=40], Person [name=Alex, age=50]]
    
    }
    
    0 讨论(0)
  • 2020-11-22 06:44

    You can use like that

    ArrayList<Group> groupList = new ArrayList<>();
    Collections.sort(groupList, Collections.reverseOrder());
    Collections.reverse(groupList);
    
    0 讨论(0)
  • 2020-11-22 06:45

    With Java8 there is a default sort method on the List interface that will allow you to sort the collection if you provide a Comparator. You can easily sort the example in the question as follows:

    testList.sort((a, b) -> Double.compare(b, a));
    

    Note: the args in the lambda are swapped when passed in to Double.compare to ensure the sort is descending

    0 讨论(0)
  • 2020-11-22 06:45

    Collections.sort allows you to pass an instance of a Comparator which defines the sorting logic. So instead of sorting the list in natural order and then reversing it, one can simply pass Collections.reverseOrder() to sort in order to sort the list in reverse order:

    // import java.util.Collections;
    Collections.sort(testList, Collections.reverseOrder());
    

    As mentioned by @Marco13, 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)

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