java - Sorting an arraylist of object base on its property in custom order -
i need sort array list in custom order (not alphabetical order) own string property.
list<weeksales> salelist = new arraylist<>(); ...
weeksales:
public class weeksales{ private string day; private int amount; // getters setters ... }
property day
day of week. ("sunday","monday", etc...) need order elements of list according order of week.("monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday")
so tried google.common.collect.ordering.
gives me error
cannot compare value: com.certus.controllers.weeksales
comparator<weeksales> com = ordering.explicit(new weeksales("monday",0), new weeksales("tuesday",0), new weeksales("wednesday",0), new weeksales("thursday",0), new weeksales("friday",0), new weeksales("saturday",0),new weeksales("sunday",0)); salelist.sort(com);
any appreciable. thank you.
update :
this question has no solution problem. have got custom order in case.
("monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday")
unless try sort list containing items listed in parameters of ordering.explicit
, doesn't know how sort them. it's not clear if you've overridden weeksales.equals
(and hashcode
), required work too.
you need tell fields compare:
class yourcomparator implements comparator<weeksales> { private final ordering<string> dayordering = ordering.explicit( "monday", "tuesday", ... ); @override public int compare(weeksales a, weeksales b) { return dayordering.compare(a.getday(), b.getday()); } }
Comments
Post a Comment