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; } } }
Comparator<DAY>and useCollections.sort(dayList, yourCustomComparator)