样品清单
ArrayList<MyObject> list = new ArrayList<MyObject>();
list.add(new MyObject (1, "2011-04-27T09:40:01.607"));
list.add(new MyObject (1, "2011-05-27T09:42:01.607"));
list.add(new MyObject (2, "2011-06-27T09:42:01.607"));
list.add(new MyObject (5, "2011-07-27T09:43:01.607"));
list.add(new MyObject (6, "2011-08-27T09:44:01.607"));
list.add(new MyObject (6, "2011-09-27T09:45:01.607"));
list.add(new MyObject (1, "2011-10-27T09:46:01.607"));
1:-如何根据ID对ArrayList进行排序
必需的输出
list.get(0)= (1, "2011-04-27T09:40:01.607"));
list.get(1)=(1, "2011-05-27T09:42:01.607"));
list.get(2)= (1, "2011-10-27T09:46:01.607"));
list.get(3)=(2, "2011-06-27T09:42:01.607"));
list.get(4)=(5, "2011-07-27T09:43:01.607"));
list.get(5)=(6, "2011-08-27T09:44:01.607"));
list.get(6)=(6, "2011-09-27T09:45:01.607"));
2:-对ID进行排序时,然后根据ID的日期对每组ID进行排序
意味着ID(1)存在3次,因此ID(1)根据其日期排序
必填输出
list.get(0)= (1, "2011-10-27T09:46:01.607"));
list.get(1)=(1, "2011-05-27T09:46:01.607"));
list.get(2)= (1, "2011-04-27T09:46:01.607"));
list.get(3)=(2, "2011-06-27T09:46:01.607"));
list.get(4)=(5, "2011-07-27T09:46:01.607"));
list.get(5)=(6, "2011-08-27T09:46:01.607"));
list.get(6)=(6, "2011-09-27T09:46:01.607"));
答案 0 :(得分:1)
您可以有2个比较器,如下所示
IdSorter.java
public class IdSorter implements Comparator<MyObject>
{
public int compare(MyObject o1, MyObject o2)
{
return o1.getId() - o2.getId();
}
}
DateSorter.java
public class DateSorter implements Comparator<MyObject>
{
public int compare(MyObject o1, MyObject o2)
{
return o1.getTime().compareTo(o2.getTime());
}
}
然后您可以按照以下顺序进行排序
Java 8
Collections.sort(list, new IdSorter()
.thenComparing(new DateSorter()));
Java 8以下
MyObjectChainedComparator.java
public class MyObjectChainedComparator implements Comparator<MyObject> {
private List<Comparator<MyObject>> listComparators;
@SafeVarargs
public MyObjectChainedComparator(Comparator<MyObject>... comparators) {
this.listComparators = Arrays.asList(comparators);
}
@Override
public int compare(MyObject o1, MyObject o2) {
for (Comparator<MyObject> comparator : listComparators) {
int result = comparator.compare(o1, o2);
if (result != 0) {
return result;
}
}
return 0;
}
}
像下面一样使用类MyObjectChainedComparator.java
Collections.sort(list, new MyObjectChainedComparator(
new IdSorter(),
new DateSorter()));
您的结果将如下所示
Id 1 Time 2011-04-27T09:40:01.607
Id 1 Time 2011-05-27T09:42:01.607
Id 1 Time 2011-10-27T09:46:01.605
Id 2 Time 2011- 06-27T09:42:01.607
Id 5 Time 2011-07-27T09:43:01.607
Id 6 Time 2011-08-27T09:44:01.607
Id 6 Time 2011-09-27T09:45:01.607
答案 1 :(得分:1)
解决此问题的另一种方法也使用此逻辑
Collections.sort(list, new MyObjectChainedComparator());
上课
public class MyObjectChainedComparator implements Comparator<MyObject> {
@Override
public int compare(MyObject o1, MyObject o2) {
int result=o1.getId()-o2.getId();
if (result==0)
result =o2.getDate().compareTo(o1.getDate());
return result;
}
}
答案 2 :(得分:0)
您应创建see -n '
/PATTERN/!{$be;d};
1h;1!H;
:e;
${g;s,^\([\n]\+\n[\n]\+\n[\n]\+\n\).*\([\n]\+\n[\n]\+\n[\n]\+\),\1\2,;p}
'
接口的新类,例如SortList
和implement
,如下所示:
Comparator
然后,对列表进行排序
class SortList implements Comparator<MyObject> {
@Override
public int compare(MyObject o1, MyObject o2) {
if (o1.id < o2.id) {// less than
return -1;
} else if (o1.id > o2.id) {// greater than
return 1;
} else {// equal
// If it is equal then compare by name
return o1.name.compareTo(o2.name);
}
}
}
结果是:
public void sort() {
ArrayList<MyObject> list = new ArrayList();
list.add(new MyObject(1, "abcd"));
list.add(new MyObject(1, "a"));
list.add(new MyObject(1, "abc"));
list.add(new MyObject(1, "ab"));
Collections.sort(list, new SortList());
for (MyObject o : list) {
System.out.println("id: " + o.id + ", name: " + o.name);
}
}
请注意:您必须更改我的代码以比较id: 1, name: a
id: 1, name: ab
id: 1, name: abc
id: 1, name: abcd
而不是Date
。我只是写了一个简单的示例,让您了解其工作原理。