我在java中有一个双精度的列表,我想按降序排序数组列表。

输入数组列表如下:

List<Double> testList = new ArrayList();

testList.add(0.5);
testList.add(0.2);
testList.add(0.9);
testList.add(0.1);
testList.add(0.1);
testList.add(0.1);
testList.add(0.54);
testList.add(0.71);
testList.add(0.71);
testList.add(0.71);
testList.add(0.92);
testList.add(0.12);
testList.add(0.65);
testList.add(0.34);
testList.add(0.62);

输出应该是这样的

0.92
0.9
0.71
0.71
0.71
0.65
0.62
0.54
0.5
0.34
0.2
0.12
0.1
0.1
0.1

当前回答

排序List的另一种方法是使用Collections框架;

在这种情况下使用SortedSet(列表中的bean应该实现Comparable,所以Double是可以的):

List<Double> testList;
...
SortedSet<Double> sortedSet= new TreeSet<Double>();
for(Double number: testList) {
   sortedSet.add(number);
}
orderedList=new ArrayList(sortedSet);

一般来说,要按列表中bean的属性排序,将列表中的所有元素放在SortedMap中,使用该属性作为键,然后从SortedMap中获取values()(该属性应该实现Comparable):

List<Bean> testList;
...
SortedMap<AttributeType,Bean> sortedMap= new TreeMap<AttributeType, Bean>();
for(Bean bean : testList) {
   sortedMap.put(bean.getAttribute(),bean);
}
orderedList=new ArrayList(sortedMap.values());

其他回答

集合。sort允许您传递一个定义排序逻辑的Comparator实例。因此,我们可以简单地传递Collections.reverseOrder()来排序,而不是按自然顺序对列表进行排序,然后反转它:

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

正如@Marco13所提到的,除了更习惯(可能更有效),使用倒序比较器可以确保排序是稳定的(这意味着当元素的顺序根据比较器相等时,它们的顺序不会改变,而倒序将改变顺序)

例如,我有一个类Person: String name, int age ==>构造函数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]]

}

如果你必须根据对象在数组列表中的id来排序,那么使用java8流。

 List<Person> personList = new ArrayList<>();

    List<Person> personListSorted =
                personList.stream()
                  .sorted(Comparator.comparing(Person::getPersonId))
                  .collect(Collectors.toList());

使用Eclipse Collections,您可以创建一个基本的双列表,对其排序,然后将其反向排列为降序。这种方法可以避免打双打。

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

如果你想要一个List<Double>,那么下面的方法可以工作。

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

如果你想保持类型为ArrayList<Double>,你可以使用ArrayListIterate实用工具类初始化和排序列表,如下所示:

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

注意:我是Eclipse Collections的提交者。

对于您的示例,这将在Java 8中发挥作用

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

但是如果你想要排序对象的一些字段,你可以很容易地通过:

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

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