我有一个Class1
public class Class1 { public Class(String s, int[] s1, int soc) { this.s = s; this.s1 = s1; this.soc = soc } }我有一个Class1的List ( List<Class1> )。 我想通过soc对这个列表进行排序,以获得最高soc第一的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 } }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; }});(请注意,比较方法返回-1表示“第一个参数在排序列表中排在第一位”,0代表“他们排序相同”,1代表“第一个参数排在排序列表第二位”,并且列表被修改按排序方法)
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)
通过其中一个变量对List我有一个Class1
public class Class1 { public Class(String s, int[] s1, int soc) { this.s = s; this.s1 = s1; this.soc = soc } }我有一个Class1的List ( List<Class1> )。 我想通过soc对这个列表进行排序,以获得最高soc第一的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 } }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; }});(请注意,比较方法返回-1表示“第一个参数在排序列表中排在第一位”,0代表“他们排序相同”,1代表“第一个参数排在排序列表第二位”,并且列表被修改按排序方法)
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)
发布评论