Aisac
Aisac

Reputation: 45

How to sort my Days List, so I can have my monday as my first Item instead of Sunday?

I have a String List with my Days of the Week, and it starts with Sunday as first item [0], how can I change the order so I have Monday as my first item of my list?

Here is my Days List:

public static List<String> Days
{
    get
    {
        return new List<string>(CultureInfo.CurrentCulture.DateTimeFormat.AbbreviatedDayNames);            
    }
}

Upvotes: 0

Views: 246

Answers (1)

Gondil
Gondil

Reputation: 807

For me this runs perfectly. It is maybe not very nice code but it do what it should do.

    public static List<String> Days
    {
        var abbDayNames = CultureInfo.CurrentCulture.DateTimeFormat.AbbreviatedDayNames;

        var days = new string[7];
        var firstDayOfWeek = (int)DayOfWeek.Monday;
        for (int i = 6; i>= 0; i--)
        {
            days[i] = abbDayNames[(firstDayOfWeek + i) % 7];
        }

        return new List<string>(days);
    }

EDIT: I edited a code little bit so it is not so stupid, and doing unnecessary things. But I believe it can be much better.

Upvotes: 1

Related Questions