I have implemented a class which implements a list of my custom DateObj
. I have sorted the list in a peculiar manner based on the current month. I achieved my desired results, but I'm required to call Collections.sort()
four times to achieve it. I feel this could be improved. Any thoughts?
//Custom Date Object public class DateObj { private int year; private int month; private int date; DateObj(int year, int month ,int date){ this.year = year; this.month = month; this.date = date; } public Integer getYear() {return year;} public Integer getMonth() {return month;} public Integer getDate() {return date;} }
The custom sort class:
import java.util.ArrayList; import java.util.Collections; import java.util.List; public class CustomListSort { static List<DateObj> list = new ArrayList<>(); public static void main(String a[]){ createRandomDateObjs(); Integer CURRENTMONTH = 6; //Sort Months Descending Collections.sort(list,(arg0,arg1)-> arg1.getMonth().compareTo(arg0.getMonth())); //Sort by keeping the object with nearest largest next month first Collections.sort(list,(arg0,arg1)->CURRENTMONTH.compareTo(arg0.getMonth())); //Sort the months which were pushed back in an ascending order. Collections.sort(list,(arg0,arg1)->(arg0.getMonth()<=CURRENTMONTH && arg1.getMonth()<=CURRENTMONTH)? arg0.getMonth().compareTo(arg1.getMonth()):0); //If months same sort them in ascending manner based on their dates Collections.sort(list, (arg0,arg1)->(arg0.getMonth() == arg1.getMonth()) ? arg0.getDate().compareTo(arg1.getDate()) : 0); System.out.println("\nDESIRED OUTPUT"); System.out.println("\nMM - DD - YYYY \n"); for(int i = 0 ; i<list.size();i++) System.out.println(list.get(i).getMonth() +" - "+list.get(i).getDate()+" - "+list.get(i).getYear()); } static void createRandomDateObjs(){ for(int i = 0 ; i<20 ; i++){ int y = 1980; y+=(int)(Math.random()*55); int m = (int) (Math.random()*11); int d = (int) (Math.random()*30); list.add(new DateObj(++y,++m,++d)); //Pre-increment to change 0's to 1's } }
Initially I had implemented it using Comparators but the Lambda made the code much cleaner.
Desired output:
MM - DD - YYYY 7 - 20 - 2023 7 - 18 - 2027 8 - 30 - 2010 8 - 12 - 2020 9 - 21 - 2006 10 - 23 - 2008 11 - 19 - 2000 11 - 16 - 2033 11 - 13 - 1989 12 - 13 - 2019 1 - 10 - 1985 1 - 15 - 2016 4 - 10 - 2021 4 - 13 - 2022 4 - 14 - 2004 5 - 1 - 2025 5 - 4 - 2014 5 - 20 - 2023 5 - 21 - 2022 6 - 29 - 1990
Comparator.thenComparing
on Java 8 to achieve this...\$\endgroup\$