Sort list model with priority id's and date time

爱⌒轻易说出口 提交于 2019-12-24 12:27:24

问题


Sample List

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:-How to Sort ArrayList with the respect of Id's

Required Output

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:-When id's are sorted then every group of id's sort according according to its date

Means id (1) exist 3 time so id (1) sort according to its date

Required output

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"));

回答1:


You can have 2 comparators like following

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());
        }
    }

Then you can sort like following

Java 8

Collections.sort(list, new IdSorter()
               .thenComparing(new DateSorter()));

Below 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;
    }
}

Use the class MyObjectChainedComparator.java like following

Collections.sort(list, new MyObjectChainedComparator(
               new IdSorter(),
               new DateSorter()));

Your result will look like this

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



回答2:


Another way to Solve this use this Logic also

Collections.sort(list, new MyObjectChainedComparator());

Make class

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;


}
 }



回答3:


You should create a new class such as SortList and implement the Comparator interface as below:

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);
        }
    }
}

And then, sorting the list like this

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);
    }
}

The result is:

id: 1, name: a
id: 1, name: ab
id: 1, name: abc
id: 1, name: abcd

Note that: you have to change my code to compare the Date instead of String. I just wrote a simple example to let you understand how it works.



来源:https://stackoverflow.com/questions/58776734/sort-list-model-with-priority-ids-and-date-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!