satory
asked on
Can you sort an ArrayList based on an Enumerations ordering?
Im trying to sort an ArrayList into the order that the elements of the ArrayList are found in my enumeration in Java?
So for example,
I have a enum class called Directions that contain 8 possible compass directions N,NE,E,SE,S,SW,W,NW and then Up and Down giving me 10 elements in this enum. Now for what I want the Up and Down directions wont come into it but since their in the same enum I thought I'd let you know about their existance.
Elsewhere in my code I have an ArrayList created from a KeySet of a hashmap of type Directions but the ArrayList is not sorted to follow a compass so im getting an ArrayList like this
[SE,NW,S,W,NE] when in reality I want something like this: [NE,SE,S,W,NW] from that same list.
So for example,
I have a enum class called Directions that contain 8 possible compass directions N,NE,E,SE,S,SW,W,NW and then Up and Down giving me 10 elements in this enum. Now for what I want the Up and Down directions wont come into it but since their in the same enum I thought I'd let you know about their existance.
Elsewhere in my code I have an ArrayList created from a KeySet of a hashmap of type Directions but the ArrayList is not sorted to follow a compass so im getting an ArrayList like this
[SE,NW,S,W,NE] when in reality I want something like this: [NE,SE,S,W,NW] from that same list.
ASKER
I was looking at the Collections API and it said it needed a List?
anyways this is what I am using:
ArrayList<Direction> aList = new ArrayList <Direction> (lookable.keySet());
aList = Collections.sort(Direction );
though its telling me that Direction cant be resolved any ideas?
anyways this is what I am using:
ArrayList<Direction> aList = new ArrayList <Direction> (lookable.keySet());
aList = Collections.sort(Direction
though its telling me that Direction cant be resolved any ideas?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
but will Collection.sort(aList) not give me a sorted ArrayList in alpha order?
I need the ArrayList(aList in the example) to be sorted in to how the Enumeration is declared.
I need the ArrayList(aList in the example) to be sorted in to how the Enumeration is declared.
ASKER
Actually your right....
For some reason I though it would sort in alpha order.
Thanks for the help!!
For some reason I though it would sort in alpha order.
Thanks for the help!!
Collections.sort(direction