简体   繁体   中英

Collection sorting in basis of two parameters simultaneously

I have a class with two date fields say:

class TestData {
    Date activation;
    Date timeStamp;
}

I want to sort the list of the above class on basis of activation date and if they are equal then on basis of timestamp ie max(activation) and max(timeStamp).

Code I tried is as follws which only fetch max(activation)

public class CollectionSort {

    public static void main(String[] args) {
        List<TestData> testList = new ArrayList<TestData>();

        Collections.sort(testList, new Comparator<TestData>() {

            @Override
            public int compare(TestData t1, TestData t2) {
                int result = 0;
                if (t1.getActivation().before(t2.getActivation())) {
                        result = 1;
                }
                return result;
            }
        });
        System.out.println("First object is " + testList.get(0));
    }
}

Any help would be greatly appreciated.

Thanks

This would do it.!

        Collections.sort(yourList, new Comparator<TestData>() {    
            public int compare(TestData o1, TestData o2) {
                int date1Diff = o1.getActivation().compareTo(o2.getActivation());
                return date1Diff == 0 ? 
                        o1.geTimestamp().compareTo(o2.getTimestamp()) :
                        date1Diff;
            }               
        });

Here's how to do it in Plain Java:

 public int compare(TestData o1, TestData o2) {
    int result = o1.getActivation().compareTo(o2.getActivation()));
    if(result==0) result = o1.getTimeStamp().compareTo(o2.getTimeStamp());
    return result;
 }

Or with Guava (using ComparisonChain ):

public int compare(TestData o1, TestData o2) {
    return ComparisonChain.start()
      .compare(o1.getActivation(), o2.getActivation())
      .compare(o1.getTimeStamp(), o2.getTimeStamp())
      .result();
 }

Or with Commons / Lang (using CompareToBuilder ):

public int compare(TestData o1, TestData o2) {
    return new CompareToBuilder()
      .append(o1.getActivation(), o2.getActivation())
      .append(o1.getTimeStamp(), o2.getTimeStamp())
      .toComparison();
 }

(All three versions are equivalent, but the plain Java version is the most verbose and hence most error-prone one. All three solutions assume that both o1.getActivation() and o1.getTimestamp() implement Comparable ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM