I have an string array
string[] days={ "1", "2", "6" }
I want to sort it according to DayOfWeek
increasingly.
For example "1" is Monday, "2" is Tuesday,"6"is saturday. Today is Thursday.
So nearest one is Saturday,Monday,Tuesday. So final array will be
days={ "6", "1", "2" }
I couln't find any solution to sort them . How can I sort them.
Thanks in advance.
String is a bit complex to explain, so I use int instead.
Let the day-of-week always not smaller than today, then compare.
int[] days = { 1, 2, 6 };
int today;
Array.Sort(days, (d1, d2) =>
{
if(d1 < today)
d1 += 7;
if(d2 < today)
d2 += 7;
return d1.CompareTo(d2);
});
Short form
Array.Sort(days, (d1, d2) => (d1 < today ? d1 + 7 : d1).CompareTo(d2 < today ? d2 + 7 : d2));