简体   繁体   English

如何对 ArrayList 进行排序?

[英]How to sort an ArrayList?

I have a List of doubles in java and I want to sort ArrayList in descending order.我在 java 中有一个双打列表,我想按降序对 ArrayList 进行排序。

Input ArrayList is as below:输入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);

The out put should be like this输出应该是这样的

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
Collections.sort(testList);
Collections.reverse(testList);

That will do what you want.那会做你想做的事。 Remember to import Collections though!不过记得导入Collections

Here is the documentation for Collections . 这是Collections的文档

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

For your example, this will do the magic in Java 8对于您的示例,这将在 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资料来源: https ://docs.oracle.com/javase/8/docs/api/java/util/Comparator.html

Use util method of java.util.Collections class, ie使用java.util.Collections类的 util 方法,即

Collections.sort(list)

In fact, if you want to sort custom object you can use事实上,如果你想对自定义对象进行排序,你可以使用

Collections.sort(List<T> list, Comparator<? super T> c) 

see collections api查看集合 api

Using lambdas (Java8), and stripping it down to the barest of syntax (the JVM will infer plenty in this case), you get:使用 lambdas (Java8),并将其剥离到最简单的语法(在这种情况下 JVM 会推断出很多),你会得到:

Collections.sort(testList, (a, b) -> b.compareTo(a));

A more verbose version:更详细的版本:

// Implement a reverse-order Comparator by lambda function
Comparator<Double> comp = (Double a, Double b) -> {
    return b.compareTo(a);
};

Collections.sort(testList, comp);

The use of a lambda is possible because the Comparator interface has only a single method to implement, so the VM can infer which method is implementing.使用 lambda 是可能的,因为 Comparator 接口只有一个方法要实现,因此 VM 可以推断出哪个方法正在实现。 Since the types of the params can be inferred, they don't need to be stated (ie (a, b) instead of (Double a, Double b) . And since the lambda body has only a single line, and the method is expected to return a value, the return is inferred and the braces aren't necessary.由于可以推断参数的类型,因此不需要说明它们(即(a, b)而不是(Double a, Double b) 。并且由于 lambda 主体只有一行,并且方法是期望返回一个值, return是推断的,大括号不是必需的。

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.在 Java8 中,List 接口上有一个默认的排序方法,如果您提供了 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注意:当传递给 Double.compare 时,lambda 中的参数会被交换,以确保排序是降序的

You can use Collections.sort(list) to sort list if your list contains Comparable elements.如果您的list包含Comparable元素,您可以使用Collections.sort(list)list进行排序。 Otherwise I would recommend you to implement that interface like here:否则,我建议您像这里那样实现该接口:

public class Circle implements Comparable<Circle> {}

and of course provide your own realization of compareTo method like here:当然也提供你自己实现的compareTo方法,如下所示:

@Override
    public int compareTo(Circle another) {
        if (this.getD()<another.getD()){
            return -1;
        }else{
            return 1;
        }
    }

And then you can again use Colection.sort(list) as now list contains objects of Comparable type and can be sorted.然后你可以再次使用Colection.sort(list)因为现在列表包含 Comparable 类型的对象并且可以排序。 Order depends on compareTo method.顺序取决于compareTo方法。 Check this https://docs.oracle.com/javase/tutorial/collections/interfaces/order.html for more detailed information.检查此https://docs.oracle.com/javase/tutorial/collections/interfaces/order.html以获取更多详细信息。

Here is a short cheatsheet that covers typical cases:这是一个涵盖典型案例的简短备忘单:

import static java.util.Comparator.comparing;

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

Collections.sort allows you to pass an instance of a Comparator which defines the sorting logic. Collections.sort允许您传递定义排序逻辑的Comparator实例。 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:因此,无需按自然顺序对列表进行排序,然后将其反转,只需将Collections.reverseOrder()传递给sort以便以相反的顺序对列表进行排序:

// 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)正如@Marco13 所提到的,除了更惯用(并且可能更有效)之外,使用逆序比较器可以确保排序是稳定的(这意味着当它们根据比较器相等时,元素的顺序不会改变,而倒车将改变顺序)

//Here is sorted List alphabetically with syncronized

package com.mnas.technology.automation.utility;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;

import org.apache.log4j.Logger;

/**
 * @author manoj.kumar
 */
public class SynchronizedArrayList {
    static Logger log = Logger.getLogger(SynchronizedArrayList.class.getName());

    @SuppressWarnings("unchecked")
    public static void main(String[] args) {

        List<Employee> synchronizedList = Collections.synchronizedList(new ArrayList<Employee>());
        synchronizedList.add(new Employee("Aditya"));
        synchronizedList.add(new Employee("Siddharth"));
        synchronizedList.add(new Employee("Manoj"));
        Collections.sort(synchronizedList, new Comparator() {
            public int compare(Object synchronizedListOne, Object synchronizedListTwo) {
                //use instanceof to verify the references are indeed of the type in question
                return ((Employee) synchronizedListOne).name
                        .compareTo(((Employee) synchronizedListTwo).name);
            }
        }); 
    /*for( Employee sd : synchronizedList) {
    log.info("Sorted Synchronized Array List..."+sd.name);
    }*/

        // when iterating over a synchronized list, we need to synchronize access to the synchronized list
        synchronized (synchronizedList) {
            Iterator<Employee> iterator = synchronizedList.iterator();
            while (iterator.hasNext()) {
                log.info("Sorted Synchronized Array List Items: " + iterator.next().name);
            }
        }

    }
}

class Employee {
    String name;

    Employee(String name) {
        this.name = name;

    }
}

if you are using Java SE 8, then this might be of help.如果您使用的是 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);

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. Collection 有一个默认的 Comparator 可以帮助你。

Also, if you want to use some Java 8 new features, you can do like that:另外,如果你想使用一些 Java 8 的新特性,你可以这样做:

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

|*| |*| Sorting an List :排序列表:

import java.util.Collections;

|=> Sort Asc Order : |=> 按升序排序:

Collections.sort(NamAryVar);

|=> Sort Dsc Order : |=> 排序 Dsc 顺序:

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

|*| |*| Reverse the order of List :颠倒 List 的顺序:

Collections.reverse(NamAryVar);

You can use like that你可以这样使用

ArrayList<Group> groupList = new ArrayList<>();
Collections.sort(groupList, Collections.reverseOrder());
Collections.reverse(groupList);

In JAVA 8 its much easy now.在 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]

-- For reverse use this -- 反向使用这个

.sorted(Comparator.reverseOrder())

For example I have a class Person: String name, int age ==>Constructor new Person(name,age)例如我有一个类 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]]

}

If you have to sort object based on its id in the ArrayList , then use java8 stream.如果您必须根据 ArrayList 中的 id 对对象进行排序,请使用 java8 流。

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

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

With Eclipse Collections you could create a primitive double list, sort it and then reverse it to put it in descending order.使用Eclipse Collections ,您可以创建一个原始的双重列表,对其进行排序,然后将其反转以按降序排列。 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> ,那么下面的方法就可以了。

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> ,可以使用ArrayListIterate实用程序类对列表进行初始化和排序,如下所示:

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

Note: I am a committer for Eclipse Collections .注意:我是Eclipse Collections的提交者。

以下行应该做粗

testList.sort(Collections.reverseOrder());

An alternative way to order a List is using the Collections framework;订购 List 的另一种方法是使用 Collections 框架;

in this case using the SortedSet (the bean in the list should implement Comparable, so Double is ok):在这种情况下使用 SortedSet(列表中的 bean 应该实现 Comparable,所以 Double 是可以的):

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

In general, to order by an attribute of a bean in the list,put all the elements of the list in a SortedMap, using as a key the attribute, then get the values() from the SortedMap (the attribute should implement Comparable):一般来说,要按列表中 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());
  yearList = arrayListOf()
    for (year in 1950 until 2021) {
        yearList.add(year)
    }

   yearList.reverse()
    val list: ArrayList<String> = arrayListOf()

    for (year in yearList) {
        list.add(year.toString())
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM