排序列表< Class>通过其中一个变量 [英] sorting List<Class> by one of its variable
本文介绍了排序列表< Class>通过其中一个变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个Class1
I have a Class1
public class Class1 {
public Class(String s, int[] s1, int soc) {
this.s = s;
this.s1 = s1;
this.soc = soc
}
}
我有列表
Class1
(列表< Class1>
)。我想按 soc
对此列表进行排序,以获得 Class
,其中 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
推荐答案
使用比较器
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;
}});
(注意,对于第一个参数在排序列表中排在第一位,compare方法返回-1, 0表示它们是相同的顺序,1表示第一个参数在排序列表中排在第二位,列表由排序方法修改)
(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)
这篇关于排序列表< Class>通过其中一个变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文