首页 > 编程知识 正文

怎么对arraylist排序,arraylist数组排序

时间:2023-05-04 20:27:36 阅读:212023 作者:256

本文翻译自: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.920.90.710.710.710.650.620.540.50.340.20.120.10.10.1#1楼

参考:https://stackoom.com/question/16BxN/如何对ArrayList进行排序

#2楼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的文档 。

#3楼

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

#4楼//Here is sorted List alphabetically with syncronizedpackage 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; }}#5楼

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)因为现在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,以获取更多详细信息。

#6楼

if you are using Java SE 8, then this might be of help. 如果您使用的是Java SE 8,则可能会有帮助。

//create a comparator object using a Lambda expressionComparator<Double> compareDouble = (d1, d2) -> d1.compareTo(d2);//Sort the Collection in this case 'testList' in reverse orderCollections.sort(testList, Collections.reverseOrder(compareDouble));//print the sorted list using method reference only applicable in SE 8testList.forEach(System.out::println);

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。