如何对List/ArrayList进行排序?

459

我有一个Java中的double列表,我想要按照降序排序ArrayList。

输入的ArrayList如下:

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

2
testList.sort(Comparator.reverseOrder()); - abbas
21个回答

6
您可以像这样做:

您可以按照以下步骤进行:

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

集合类有一个默认的比较器可以帮助你完成这个操作。

另外,如果你想使用一些Java 8的新特性,可以像下面这样做:

List<String> yourList = new ArrayList<String>();
yourList = yourList.stream().sorted(Collections.reverseOrder()).collect(Collectors.toList());

5
如果你正在使用Java SE 8,那么这可能会有所帮助。
//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);

6
还有一个Collections.reverseOrder()的方法,不需要任何参数,它可以使你实现的compareDouble方法变得多余(等同于Double的自然排序)。这里的答案应该是Collections.sort(testList, Collections.reverseOrder()); - Matt

5

|*| 列表排序 :

import java.util.Collections;

|=> 升序排序:

Collections.sort(NamAryVar);

|=> 排序为降序 :

Collections.sort(NamAryVar, Collections.reverseOrder());

|*| 反转列表的顺序:

Collections.reverse(NamAryVar);

5
在Java 8中,现在变得更加容易了。
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]

-- 为反向使用,请使用以下内容

.sorted(Comparator.reverseOrder())

4
你可以这样使用
ArrayList<Group> groupList = new ArrayList<>();
Collections.sort(groupList, Collections.reverseOrder());
Collections.reverse(groupList);

3
例如,我有一个名为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]]

}

if you want to short by name -> if you want to sort by name - linrongbin

3

如果您需要根据ArrayList中的id对对象进行排序,则使用Java8流。

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

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

2
使用Eclipse Collections,您可以创建一个原始的double列表,对其进行排序,然后将其反转以使其按降序排列。这种方法可以避免装箱双倍。
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的提交者。

0

下面这行代码应该可以解决问题

testList.sort(Collections.reverseOrder());

0

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

在这种情况下,可以使用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());

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接