Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

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


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.