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.
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);
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With