Reputation: 241
I want to sort a collection on basis of enum order, but getting error : The method sort(List, Comparator<? super T>) in the type Collections is not applicable for the arguments
The classes are as following This is the enum class on whos order I need the sorting
package com;
import java.util.Comparator;
public enum MyLink {
CREATE_LOGIN("My Create login" ,0),
LISTEN_NOW("My Listen now", 1),
FREE_PREVIEW ("My FREE PREVIEW", 2);
private final String value;
private final int order;
private MyLink(String value, int order) {
this.value = value;
this.order = order;
}
public String getValue(){
return value;
}
public int getOrder() {
return order;
}
public static Comparator<MyLink> orderComparator = new Comparator<MyLink>() {
public int compare(MyLink ctaLink1, MyLink ctaLink2) {
return ctaLink1.getOrder() - ctaLink2.getOrder();
}
};
}
This class conatins the MyLink Order.
package com public class Profile {
private MyLink eligibilityType;
private String id;
public MyLink getEligibilityType() {
return eligibilityType;
}
public void setEligibilityType(MyLink eligibilityType) {
this.eligibilityType = eligibilityType;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
@Override
public String toString() {
return "Profile [eligibilityType=" + eligibilityType + ", id=" + id + "]";
}
}
Here In TestTrial I am setting the list and calling the collections.sort mwthod which is throwing error Getting error The method sort(List, Comparator<? super T>) in the type Collections is not applicable for the arguments
package com;
import java.util.List;
import java.util.ArrayList;
import java.util.Collections;
import java.util.*;
public class TestTrial {
public static void main(String[] args) {
System.out.println("hello");
List<Profile> prolifes = new ArrayList<Profile>();
Profile n1 = new Profile();
n1.setId("2");
n1.setEligibilityType(MyLink.FREE_PREVIEW);
prolifes.add(n1);
Profile n2 = new Profile();
n2.setId("1");
n2.setEligibilityType(MyLink.CREATE_LOGIN);
prolifes.add(n2);
Profile n3 = new Profile();
n3.setId("3");
n3.setEligibilityType(MyLink.LISTEN_NOW);
prolifes.add(n3);
for(Profile n : prolifes) {
System.out.println("n::" + n );
}
//Collections.sort(nonPiiSubscriptions, CTALink.orderComparator);
//nonPiiSubscriptions.sort(Comparator.comparing(nonPiiSubscriptions::name));
Collections.sort(prolifes, new Comparator<MyLink>()
{
@Override
public int compare(MyLink ctaLink1,MyLink ctaLink2)
{
return ctaLink1.getOrder() - ctaLink2.getOrder();
}
});
}
}
Please suggest how I can sort the list of profile on enum order
Upvotes: 1
Views: 2216
Reputation: 397
You are trying to sort a List of Profile but your Comparator compares MyLink - not Profile. The Comparator should be something like (not tested):
new Comparator<Profile>() {
@Override
public int compare(Profile ctaLink1, Profile ctaLink2) {
return ctaLink1.getEligibilityType().getOrder() - ctaLink2.getEligibilityType().getOrder();
}
}
Of course for sake of clarity you should have a Profile Comparator that forwards to your MyLink Comparator
Upvotes: 1