views:

34

answers:

2

Hi, how do i order a given set of colors from the rainbow in VIBGYOR order. say i input the seven colors in the following order { red, blue, green, yellow, indigo, violet, orange} and i should print the output as {violet, indigo, blue, green, yellow, orange, red}, irrespective of the order i give the output should be as above. Can someone suggest me about implementing this in java program?
Thanks,
-Vijay

A: 
  1. Create a class that represents a color

  2. Implement the Comparable interface (possibly by using the getHSBColor).

    Basically this means, "make sure one color can be compared to any other color".

  3. Put the colors in a List and use Collections.sort (or an array and use Arrays.sort).

aioobe
A: 

I'm assuming that either you will get less than 7 inputs or multiples of some values on occasion. If you always get the exact same inputs, just in different orders, the problem is trivial. :-)

You might want to think about using an enum corresponding to each color and translating the input to an array of these enums. The value of the enum should reflect the output order. Use this to sort the array and then output the enum names.

tvanfosson
Thanks for the suggestion. I predicted to use enum but how was the question. Tried out a snippet code and got it clarified.
Vijay Selvaraj