Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I want to sort the months of the year starting at any month, but January needs to show up after December if January isn't the starting point. Can I do that in an array or do I need a special data structure or algorithm to do that?

share|improve this question
    
how sorting order is controlled, design or run-time? –  Yusubov Jul 3 '12 at 13:45

3 Answers 3

you can easily do this with just an array. [Dec,Jan,Feb,...Oct,Nov] then you can iterate through with month mod 12 and it will produce a sorted list that can wrap around from Dec to Jan. You can simply stop iterating at January if you only want that month to wrap.

share|improve this answer

Check this Java-ish code:

public void sortMonths(int START) {
    // Jan = 0, ... , Dez = 11
    int[] myArray = new int[12];
    for (int i = START; i < 12; ++i) {
        myArray[i - START] = i;
    }

    for (int i = 0; i < START; ++i) {
        myArray[i] = i;
    }
}
share|improve this answer
    
care to elaborate for down votes? –  mcwise Jul 3 '12 at 17:53

Many sorters let you provide your own comparator which dictates the sort order. Write one that implements your rules and and supply it to the sort function.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.