0
public static void main(String[] args) { // TODO Auto-generated method stub sortDates(Arrays.asList(new DAY[]{DAY.MON,DAY.WED,DAY.TUE,DAY.THU,DAY.SUN,DAY.SAT,DAY.FRI})); } public static void sortDates(List<DAY> dayList){ Set<DAY> set = new TreeSet<DAY>(dayList); for(DAY day : set) { System.out.println(day); } } public enum DAY { MON("MONDAY"),TUE("TUESDAY"),WED("WEDNESDAY"),THU("THURSDAY"),FRI("FRIDAY"),SAT("SATURDAY"),SUN("SUNDAY"); private String m_name; DAY(String name) { m_name=name; } } 

The out put for this piece of code is : MON TUE WED THU FRI SAT SUN

But I want the output is like this SUN MON TUE WED THU FRI SAT

That's mean I want to customize the sorting function to move Sunday to the first.

How can i do this? Thanks in advanced.

4
  • Can you define this as a treemap and assign keys as the sortable field? Commented Mar 6, 2014 at 16:10
  • out of curiosity: why do you skip friday? hack: reorder the days' declarations in you enum DAY accordingly guessed solution: supply a custom Comparator<DAY> and use Collections.sort(dayList, yourCustomComparator) Commented Mar 6, 2014 at 16:10
  • Provide a Comparator to the TreeSet constructor. Commented Mar 6, 2014 at 16:10
  • Sorry , my mistake for missing FRIDAY, i have edited my question Commented Mar 6, 2014 at 16:14

6 Answers 6

3

Try this.

 import java.util.*; public class SortDays { public static void main(String[] args) { // TODO Auto-generated method stub sortDates(Arrays.asList(new DAY[]{DAY.MON,DAY.WED,DAY.TUE,DAY.THU,DAY.SUN,DAY.SAT})); } public static void sortDates(List<DAY> dayList){ Collections.sort(dayList, new Comparator<DAY>() { public int compare(DAY day1, DAY day2) { return day1.getWeight() - day2.getWeight(); }}); System.out.println("sortedlist is" + dayList.toString()); } } enum DAY { MON("MONDAY", 2) ,TUE("TUESDAY", 3),WED("WEDNESDAY", 4),THU("THURSDAY", 5),FRI("FRIDAY", 6),SAT("SATURDAY", 7),SUN("SUNDAY", 1); private String m_name; private int m_weight; DAY(String name, int weight) { m_name=name; m_weight = weight; } public int getWeight() { return this.m_weight; } } 
Sign up to request clarification or add additional context in comments.

Comments

1

@Marco13's answer is the most correct so far: you need a custom comparator that enforces your ordering.

Luckily, Google Guava has a utility for that: Ordering.explicit().

private static Comparator<DAY> AM_ORD = Ordering.explicit(DAY.Sun, ... DAY.Sat); private static Comparator<DAY> EU_ORD = Ordering.explicit(DAY.Mon, ... DAY.Sun); public static void sortDates(List<DAY> dayList, boolean am) { Set<DAY> set = new TreeSet<DAY>(am ? AM_ORD : EU_ORD); set.addAll(dayList); for (DAY day : set) { System.out.println(day); } } 

1 Comment

This should be the answer. Good stuff greyfairer.
0

Following approach (with a comparator) needs an identifier in the enum class:

import java.util.Arrays; import java.util.Comparator; import java.util.List; import java.util.Set; import java.util.TreeSet; public class week { public static void main(String[] args) { Comparator<DAY> myComparator = new Comparator<DAY>() { @Override public int compare(DAY o1, DAY o2) { if (o1.getPrio() == o2.getPrio()) { return 0; } else if (o1.getPrio() < o2.getPrio()) { return -1; } return 1; } }; sortDates(Arrays.asList(new DAY[] { DAY.MON, DAY.WED, DAY.TUE, DAY.THU, DAY.FRI, DAY.SUN, DAY.SAT }), myComparator); } public static void sortDates(List<DAY> dayList, Comparator<DAY> comparator) { Set<DAY> setd = new TreeSet<DAY>(comparator); setd.addAll(dayList); Set<DAY> set = new TreeSet<DAY>(dayList); for (DAY day : set) { System.out.println(day); } System.out.println("comparator:"); for (DAY day : setd) { System.out.println(day); } } public enum DAY { MON("MONDAY", 1), TUE("TUESDAY", 2), WED("WEDNESDAY", 3), THU("THURSDAY", 4), FRI("FRIDAY", 5), SAT("SATURDAY", 6), SUN("SUNDAY", 0); private final String m_name; private final int prio; DAY(String name, int prio) { m_name = name; this.prio = prio; } public int getPrio() { return prio; } } } 

Comments

0

The only constant assumption you can have is the sequence of the days. We know that after Monday is Tuesday and after it is Wednesday.

public static final void printWeek(Day firstDayOfWeek) { Day[] days = Day.values(); int offset = firstDayOfWeek.ordinal(); for(int i = 0; i< days.length; i++) { System.out.println(days[offset % days.length]); offset++; } } 

Thanks to this you will be able to create order not only from Monday or Sunday but from every day of the week.

Good luck.

Comments

0

I'd not recommend to add another field ("priority" or "weight") to the enum - simply because it is not necessary and does not make things easier to maintain. Instead, you can create a Comparator that simply treats "Sunday" as the first day:

private static Comparator<DAY> createComparator() { return new Comparator<DAY>() { @Override public int compare(DAY d0, DAY d1) { if (d0 == DAY.SUN) { if (d1 == DAY.SUN) { return 0; } else { return -1; } } else if (d1 == DAY.SUN) { return 1; } return d0.ordinal() - d1.ordinal(); } }; } public static void sortDates(List<DAY> dayList) { Set<DAY> set = new TreeSet<DAY>(createComparator()); set.addAll(dayList); for (DAY day : set) { System.out.println(day); } } 

Comments

0

By default a TreeSet will use the natural ordering of the supplied objects. For an Enum this is based on the ordinal value for the enum, and this is set according to the order that the enumerated values are declared. So if you chose to declare the DAY enum in the order that you wish to display (SUNDAY, MONDAY…) then the natural order would be as you desire.

An alternative would be to supply an implementation of Comparator to the TreeSet on construction and then you can define a customer order. In the example below I've added an index field to your DAY enum and provided a comparator that looks at the index. It isn't necessary to provide the index and this does tie the order of the days of the week into the DAY enum and that may not be desired.

import java.util.Arrays; import java.util.Comparator; import java.util.List; import java.util.Set; import java.util.TreeSet; public class Question { public static void main(final String[] args) { // TODO Auto-generated method stub sortDates(Arrays.asList(new DAY[] { DAY.MON, DAY.WED, DAY.TUE, DAY.THU, DAY.SUN, DAY.SAT, DAY.FRI })); } public static void sortDates(final List<DAY> dayList) { final Set<DAY> set = new TreeSet<DAY>(); set.addAll(dayList); for (final DAY day : set) { System.out.println(day); } } public enum DAY implements Comparable<DAY> { MON("MONDAY", 1), TUE("TUESDAY", 2), WED("WEDNESDAY", 3), THU( "THURSDAY", 4), FRI("FRIDAY", 5), SAT("SATURDAY", 6), SUN( "SUNDAY", 0); private final String m_name; private final int m_index; DAY(final String name, final int index) { m_name = name; m_index = index; } } public static class DAYComparator implements Comparator<DAY> { @Override public int compare(final DAY o1, final DAY o2) { int returnValue = 0; if (o1.m_index > o2.m_index) { returnValue = 1; } else if (o1.m_index < o2.m_index) { returnValue = -1; } return returnValue; } } } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.