Answer by fge for How to sort a list of objects
Use TimeUnit to replace your Unit enum.Your Term class could therefore have a .toMillis() method which is very simple:public final long toMillis(){ return timeUnit.toMillis(value);}You can then just...
View ArticleAnswer by santosh Kumar for How to sort a list of objects
Please use the compareto function as below:public int compareTo(Object o) { Test t1 = (Test)this; Test t2 = (Test)o; if(t1.getValue()*(t1.getUnit().getValue()+1) >...
View ArticleAnswer by Sumit for How to sort a list of objects
You'll first have to check on the basis of Unit and then on the basis of Value in the overridden compareTo(). See here the implementation of sorting on the basis of lastname and then...
View ArticleAnswer by wassgren for How to sort a list of objects
If you simply want to compare according to your example the following code solves the problem:Collections.sort(terms, new Comparator<Test>() { @Override public int compare(final Test o1, final...
View ArticleAnswer by wgitscht for How to sort a list of objects
If you really want to do it that way, assign your enums values like so:DAY(1), WEEK(7), MONTH(31), YEAR(365)than multiply in your comperator Unit.getValue() * value e.g. 4 MONTH gives you 31 * 4 = 124...
View ArticleAnswer by chiastic-security for How to sort a list of objects
You'll need to convert first into common format, and then compare. Since days are your smallest type, you should convert the two instances you're comparing into a number of days, and then compare based...
View ArticleHow to sort a list of objects
Supposing that I have a list of Test as below: public class Test { public static enum Unit { DAY, WEEK, MONTH, YEAR /* , DAY_TO_DAY */ } private int value; private Unit unit; public int getValue() {...
View Article