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.


satoryAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

objectsCommented:
sure you can just use

Collections.sort(directions);
0
satoryAuthor Commented:
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?
0
objectsCommented:
> aList = Collections.sort(Direction);

should be:

Collections.sort(aList);

If its the Map keys that you want to sort on then using a TreeMap is another option as it will keep your Map sorted on key
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
satoryAuthor Commented:
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.

0
satoryAuthor Commented:
Actually your right....


For some reason I though it would sort in alpha order.  


Thanks for the help!!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.