java 按其变量之一对 List<Class> 进行排序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4018090/
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
sorting List<Class> by one of its variable
提问by y2p
I have a Class1
我有一个 Class1
public class Class1 {
public Class(String s, int[] s1, int soc) {
this.s = s;
this.s1 = s1;
this.soc = soc
}
}
I have a List
of Class1
(List<Class1>
). I want to sort this list by soc
, to get the Class1
with highest soc
first
我有一个List
的Class1
(List<Class1>
)。我想这个排序列表中soc
,以获得Class1
最高soc
第一
回答by Scott Stanchfield
Use a Comparator
使用比较器
Collections.sort(list, new Comparator<Class1>() {
public int compare(Class1 c1, Class1 c2) {
if (c1.soc > c2.soc) return -1;
if (c1.soc < c2.soc) return 1;
return 0;
}});
(Note that the compare method returns -1 for "first argument comes first in the sorted list", 0 for "they're equally ordered" and 1 for the "first argument comes second in the sorted list", and the list is modified by the sort method)
(请注意,compare 方法返回 -1 表示“第一个参数在排序列表中排在第一位”,0 表示“它们的顺序相同”,1 表示“第一个参数在排序列表中排在第二位”,并且列表被修改通过排序方法)
回答by aioobe
Here is a complete example:
这是一个完整的例子:
import java.util.*;
class Class1 {
String s;
int[] s1;
int soc;
public Class1(String s, int[] s1, int soc) {
this.s = s;
this.s1 = s1;
this.soc = soc;
}
public String toString() { return String.format("s: %s soc: %d", s, soc); }
}
public class Test {
public static void main(String... args) {
List<Class1> list = new ArrayList<Class1>();
list.add(new Class1("abcd", new int[] {1}, 3));
list.add(new Class1("efgh", new int[] {2}, 5));
list.add(new Class1("ijkl", new int[] {8}, 9));
list.add(new Class1("mnop", new int[] {3}, 7));
Collections.sort(list, new Comparator<Class1>() {
public int compare(Class1 o1, Class1 o2) {
return o1.soc > o2.soc ? -1 : o1.soc == o2.soc ? 0 : 1;
}
});
System.out.println(list.toString().replaceAll(",", "\n"));
}
}
It prints the following:
它打印以下内容:
[s: ijkl soc: 9
s: mnop soc: 7
s: efgh soc: 5
s: abcd soc: 3]
回答by ColinD
While Scott Stanchfield's answer is generally the easiest way to do this in Java currently, if you have other functional things you might want to do with properties of your class it can be useful to make use of Guava's Functions.
虽然 Scott Stanchfield 的答案通常是目前在 Java 中执行此操作的最简单方法,但如果您有其他功能性的事情,您可能想要对类的属性进行处理,那么使用Guava的Function可能会很有用。
public class Class1 {
...
public static final Function<Class1, Integer> GET_SOC =
new Function<Class1, Integer>() {
public Integer apply(Class1 input) {
return input.soc;
}
};
...
}
Then you can use its Orderingclass to sort:
然后你可以使用它的Ordering类来排序:
List<Class1> list = ...;
Collections.sort(list, Ordering.natural().reverse().onResultOf(Class1.GET_SOC));
This uses the reverse of the natural ordering based on the soc
property of each Class1
instance to give the ordering you want.
这使用基于soc
每个Class1
实例的属性的自然排序的反向来给出您想要的排序。
回答by MattC
Create a class that implements Comparator, create your custom sorting method and then pass an instance of that class into this function: Collections.sort
创建一个实现Comparator的类,创建自定义排序方法,然后将该类的一个实例传递给这个函数:Collections.sort