Custom sort on ArrayList for days of the week

Viewed 714

I'm looking to sort an arraylist of strings to make them so all the mondays are at the start of the list and all the fridays are at the end. For example,

days.add("Thursday");
days.add("Monday");
days.add("Friday");
days.add("Wednesday");
days.add("Thursday");
days.add("Thursday");

I would like this to be outputted as follows;

"Monday"
"Wednesday"
"Thursday"
"Thursday"
"Thursday"
"Friday"

I am doing this as I need to graph the results and I'd prefer if the graph was in this order. I was thinking maybe using a mapping to put Monday, 1 : Tuesday, 2 etc and compare that way but I don't know if there is a simpler way. Any help is greatly appreciated Thanks!

5 Answers

You can use Enum for days of the week, becouse underneath each of te enums have their own value.

public enum DayOfTheWeek {
    Monday, //default value of 0
    Tuesday, //default value of 1
    Wednesday, //default value of 2
    Thursday, 
    Friday,
    Saturday,
    Sunday
}

Then just make ArrayList of DayOfTheWeek and use Collections.sort().

    ArrayList<DayOfTheWeek> days = new ArrayList<>();
    days.add(DayOfTheWeek.valueOf("Friday"));
    days.add(DayOfTheWeek.valueOf("Monday"));
    days.add(DayOfTheWeek.valueOf("Saturday"));
    days.add(DayOfTheWeek.valueOf("Sunday"));
    Collections.sort(days);

You can use custom sorting through either implementing Comparable interface method compareTo or Comparator interface compare method
This should help
Sort ArrayList of custom Objects by property

Update : you can make a hashmap like this

HashMap<String , Integer> rank = new HashMap<>();
rank.put("Monday" , 1);
rank.put("Tuesday" , 2);
.
.
and so on..

now just use custom comparator where you sort on the basis of rank.get(currentDay)

Collections.sort(days , new thisway(rank));

then implement the Comparator interface compare method

class thisway implements Comparator<String>{

    private HashMap<String, Integer> rank;

    thisway(HashMap<String , Integer> hashmap){
        this.rank = hashmap;
    }

    @Override
    public int compare(String a , String b){
       if(rank.get(a).intValue() < rank.get(b).intValue()){
           return -1;
       }
       if(rank.get(a).intValue() == rank.get(b).intValue()){
           return 0;
       }
       return 1;
    }
}

I guess this should help.

You can define a custom Comparator using the class DayOfWeek in this way:

Comparator<String> comparator = new Comparator<String>() {

    @Override
    public int compare(String day1, String day2) {
        return Integer.compare(DayOfWeek.valueOf(day1.toUpperCase()).getValue(),
                               DayOfWeek.valueOf(day2.toUpperCase()).getValue());
    }
};

After in your code you can call it like below:

List<String> days = new ArrayList<String>();
days.add("Thursday");
days.add("Monday");
days.add("Friday");
days.add("Wednesday");
days.add("Thursday");
days.add("Thursday");
Collections.sort(days, comparator);
System.out.println(days); // will print [Monday, Wednesday, Thursday, Thursday, Thursday, Friday]
days.sort(
    Comparator.comparing(day -> DayOfWeek.valueOf(day.toString().toUpperCase()))
);

Something like that will do easily:

days.sort(Comparator.comparing(day -> List.of("Monday","Tuesday","Wednesday", "Thursday","Friday","Satursday","Sunday").indexOf(day)));

But make List.of("Monday","Tuesday","Wednesday", "Thursday","Friday","Satursday","Sunday") a constant to avoid recreating it everytime.

Related