I wanted to sort the list based on two properties but on certain conditions. The below class has two properties - memberid,membernumber. And the values are as below.
obj ()
{
memberid;
membernumber;
}
Values Assigned :
obj1 = {1,15}
obj2 = {3,10}
obj3 = {2,20}
obj4 = {2,10}
obj5 = {3,15}
obj6 = {4,25}
obj7 = {2,50}
We need the sorting as:
First sort it based on memberId
, and if member Id are same sort it based on membernumber
highest membernumber
should come first. so the output should be :
obj1 = {1,15}
obj7 = {2,50} >> As 2 are multiple sort on membernumber as highest being 50
obj3 = {2,20}
obj4 = {2,10}
obj5 = {3,15}
obj2 = {3,10}
obj6 = {4,25}
I am able to sort it based on MemberId using collections.sort, But am not sure how to have that equal condition and sort it based on MemberNumber.
Any help is appreciated.
Sorting class which I am using in Collections.sort()
.
public class sortId implements Comparator<obj > {
public int compare(obj o1, obj o2)
{
int id1 = o1.getmemberId();
int id2 = o2.getmemberId();
return id1 - id2 ;
}
}