Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to sort an array of week days so that they appear as in a calendar week (in C#)?

Tags:

arrays

c#

I have a text file being read into a string[] containing the days Monday to Friday, but in a random order. I want to sort them so they appear as they do in a calendar week.

I've tried Array.Sort() and Array.Reverse() but they don't work.

Any ideas?

like image 335
NFI78 Avatar asked Nov 14 '25 13:11

NFI78


1 Answers

Just sort it by the corresponding value of enum DayOfWeek of your string, then return it back to a string.

string [] initialArray = {"Friday", "Monday", ... } ; 

string [] sortedArray = initialArray.OrderBy(s => Enum.Parse(typeof(DayOfWeek), s)).ToArray() ; 
like image 80
Perfect28 Avatar answered Nov 17 '25 03:11

Perfect28



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!