Java 如何在android中按升序或降序对数组列表进行排序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18657999/
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 array list in ascendin or descending order values in android?
提问by Yugesh
My array-list value is like below
我的数组列表值如下所示
[
{
Distance=Distance: 16.6km,
branch_desc=name 1,
},
{
Distance=Distance: 5.4km,
branch_desc=name 2,
},
{
Distance=Distance: 13.4km,
branch_desc=name 3,
}
]
How to sort the array-list to ascending and descending order compare with distance.I don't know how to sort the array list.Can any one know please help me to solve this issue.
如何将数组列表按升序和降序排序与距离进行比较。我不知道如何对数组列表进行排序。有谁知道请帮我解决这个问题。
回答by Developer
Try This Code
试试这个代码
List<List<Integer>> list = Arrays.asList(Arrays.asList(10, 5, 4),
Arrays.asList(1, 2, 3), Arrays.asList(5, 6, 7));
for (List<Integer> l : list) {
Collections.sort(l);
}
Collections.sort(list, new Comparator<List<Integer>>() {
public int compare(List<Integer> o1, List<Integer> o2) {
return o1.get(0).compareTo(o2.get(0));
}
});
System.out.println(list);
String Sort
字符串排序
Collections.sort(list, new Comparator<String>() {
@Override
public int compare(String s1, String s2) {
return s1.compareToIgnoreCase(s2);
}
});
回答by Arnaud Denoyelle
See Collections#sort(List<T> list)
. It helps you to sort any list of T
which implements Comparable.
见Collections#sort(List<T> list)
。它可以帮助您对T
实现Comparable 的任何列表进行排序。
If your class does not implement Comparable, you can provide a Comparator and call static void <T> sort(List<T> list, Comparator<? super T> c)
.
如果您的类没有实现 Comparable,您可以提供一个 Comparator 并调用static void <T> sort(List<T> list, Comparator<? super T> c)
.
Example with a small class:
小班级示例:
public class Item implements Comparable<Item>{
private Integer number;
private Double distance;
//Getters & Setters
...
@Override
public int compareTo(Item o) {
return this.distance > o.distance? -1 : 1;
}
}
As it implements Comparable, you can sort a list like this :
当它实现 Comparable 时,您可以像这样对列表进行排序:
List<Item> myItemList = ... ;
Collections.sort(myItemList);
//Now, myItemList is sorted
ORyou can provide a Comparator to sort items on any criteria :
或者您可以提供一个 Comparator 来根据任何条件对项目进行排序:
List<Item> list = ...;
//Here, I provide a Comparator to sort Items by number.
Collections.sort(list, new Comparator<Item>() {
@Override
public int compare(Item o1, Item o2) {
return o2.getNumber() - o1.getNumber();
}
});
回答by flawyte
I suggest you to create a dedicated Comparator
class extension.
You can whether create one single class and use a boolean to specify if this should be ascending or descending order, or create one class for each order.
我建议您创建一个专用的Comparator
类扩展。您可以创建一个类并使用布尔值来指定是升序还是降序,或者为每个订单创建一个类。
The difference will reside in your implementation(s) of compare(T, T), because this is where you will actually compare objects with criteria you choose, which is in your case the value of the field Distance
.
Then you call Collections.sort(ArrayList, Comparator)to sort the ArrayList
.
差异将存在于您的compare(T, T) 实现中,因为这是您实际将对象与您选择的标准进行比较的地方,在您的情况下是 field 的值Distance
。然后调用Collections.sort(ArrayList, Comparator)对ArrayList
.
Using one single class
使用一个单一的类
MyComparator comp = ... // You can even pass the boolean at instanciation
comp.setAscending(true); // 'false' for descending
Collections.sort(list, comp); // Sort is performed - order depends on boolean
Using 2 casses
使用 2 个案例
AscendingComparator comp1 ...
DescendingComparator comp2 ...
Collections.sort(list, comp1); // Sort ascending
Collections.sort(list, comp2); // Sort descending