Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sorting a list of object based on the property date sudo object values

Tags:

java

android

I have a class named SomeClass which has two properties: SomeDateObject and a list of SomeClass2. I wants to sort a given list of SomeClass objects on the basis of SomeDateObject. by means wants to sort

 public List<SomeClass  > someClassList

How to achieve the sorting based on SomeDateObject

The structure of SomeClass is given as below :

 public class SomeClass  {


        public SomeDateObject date;

        public List<SomeClass2> some2;

        public SomeClass  () {
        }
    }

The structure of SomeDateObject is given as below

public class SomeDateObject {

        public int day;
        public int month;
        public int year;
}

What can be the best way to sort the list of SomeClass objects based on SomeDateObjects. as we can use Comparator and comparables but SomeDateObject is not a straight forward date but is having date details. Rather than using a straight Comparator can we go for guava-libraries.

like image 209
Jitesh Upadhyay Avatar asked Jan 24 '26 11:01

Jitesh Upadhyay


1 Answers

You can use Collections.sort with a Comparator like this,

Collections.sort(SomeClassList, new Comparator<T>() { 
\\ T is a type parameter, you need to pass type argument here

@Override
public int compare(T lhs, T rhs) {
        return lhs.compareTo(rhs);
    }
});
like image 166
Sufiyan Ghori Avatar answered Jan 25 '26 23:01

Sufiyan Ghori



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!