Java 如何对 List<Integer> 进行排序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20518078/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to sort List<Integer>?
提问by
I have code and I have used list to store data. I want to sort data of it, then is there any way to sort data or shall I have to sort it manually by comparing all data?
我有代码,并且使用列表来存储数据。我想对它的数据进行排序,那么有没有办法对数据进行排序,还是必须通过比较所有数据来手动对其进行排序?
import java.util.ArrayList;
import java.util.List;
public class tes
{
public static void main(String args[])
{
List<Integer> lList = new ArrayList<Integer>();
lList.add(4);
lList.add(1);
lList.add(7);
lList.add(2);
lList.add(9);
lList.add(1);
lList.add(5);
for(int i=0; i<lList.size();i++ )
{
System.out.println(lList.get(i));
}
}
}
采纳答案by Java Curious ?
You can use Collections
for to sort data:
您可以使用Collections
for 对数据进行排序:
import java.util.Collections;
import java.util.ArrayList;
import java.util.List;
public class tes
{
public static void main(String args[])
{
List<Integer> lList = new ArrayList<Integer>();
lList.add(4);
lList.add(1);
lList.add(7);
lList.add(2);
lList.add(9);
lList.add(1);
lList.add(5);
Collections.sort(lList);
for(int i=0; i<lList.size();i++ )
{
System.out.println(lList.get(i));
}
}
}
回答by Batty
Use Collections class API to sort.
使用 Collections 类 API 进行排序。
Collections.sort(list);
回答by Ruchira Gayan Ranaweera
回答by Bosko Mijin
You are using Lists, concrete ArrayList. ArrayList also implements Collection interface. Collection interface has sort method which is used to sort the elements present in the specified list of Collection in ascending order. This will be the quickest and possibly the best way for your case.
您正在使用列表,具体的 ArrayList。ArrayList 还实现了 Collection 接口。Collection 接口有 sort 方法,用于按升序对 Collection 指定列表中存在的元素进行排序。对于您的情况,这将是最快的,也可能是最好的方法。
Sorting a list in ascending order can be performed as default operation on this way:
按升序对列表进行排序可以作为默认操作以这种方式执行:
Collections.sort(list);
Sorting a list in descending order can be performed on this way:
可以通过这种方式按降序对列表进行排序:
Collections.reverse(list);
According to these facts, your solution has to be written like this:
根据这些事实,您的解决方案必须这样写:
public class tes
{
public static void main(String args[])
{
List<Integer> lList = new ArrayList<Integer>();
lList.add(4);
lList.add(1);
lList.add(7);
lList.add(2);
lList.add(9);
lList.add(1);
lList.add(5);
Collections.sort(lList);
for(int i=0; i<lList.size();i++ )
{
System.out.println(lList.get(i));
}
}
}
More about Collections you can read here.
您可以在此处阅读有关收藏的更多信息。
回答by Kamlesh Arya
Ascending order:
升序:
Collections.sort(lList);
Descending order:
降序排列:
Collections.sort(lList, Collections.reverseOrder());
回答by rachana
To sort in ascending order :
要按升序排序:
Collections.sort(lList);
And for reverse order :
而对于逆序:
Collections.reverse(lList);
回答by Anugoonj
You can use the utility method in Collections
class
public static <T extends Comparable<? super T>> void sort(List<T> list)
or
您可以在Collections
类中
使用实用程序方法public static <T extends Comparable<? super T>> void sort(List<T> list)
或
public static <T> void sort(List<T> list,Comparator<? super T> c)
Refer to Comparable
and Comparator
interfaces for more flexibility on sorting the object.
请参阅Comparable
和Comparator
接口以更灵活地对对象进行排序。