locked
sort a list of weekdays chronologically. RRS feed

  • Question

  • i have an array of strings that looks like this

    Friday
    Monday
    Saturday
    Thursday
    Friday
    Wednesday
    Sunday
    Tuesday
    Sunday
    Monday

    I want to sort them so they appear chronologically Monday through Sunday.

    Monday, April 8, 2019 6:37 PM

Answers

  • That will work but it only needs to  be like this:

    enum order{
        Monday
        Tuesday
        Wednesday
        Thursday
        Friday
        Saturday
        Sunday
    }
    

    The default is the index and only needs an explicit value if you want the value to be other than the ordinal.


    \_(ツ)_/

    Tuesday, April 9, 2019 1:46 AM
  • right, except that the integer value doesn't meet the "Monday through Sunday" requirement if the integer value of Sunday is 0 and Saturday is 6.

    I found the array solution here

    https://stackoverflow.com/questions/48701384/powershell-sorting-in-custom-order

    and this code does what I need it to do, without changing any culture or calendar settings:

    $order = @('Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday')
    
    $sortedschedulearray = foreach($day in $order)
        {
        $scheduleArray | ? {$_ -match $day}
        } 

    Tuesday, April 9, 2019 12:10 AM

All replies

  • Convert the strings to the enum int value and sort the ints.

    [int][dayofweek]'Tuesday'


    \_(ツ)_/

    Monday, April 8, 2019 7:27 PM
  • thank you for that. how might I programmatically use that through my example array with no column headings?

    Monday, April 8, 2019 8:28 PM
  • ok I lied and my strings are more complicated than that, but they do start with the days of the week. I just had to split them, and the dates get sorted.

    $schedulearray | sort-property @{Expression={([int][dayofweek]($_.split(" ")[0]))}}

    Sunday every 0 minutes from 00:00 to 00:00
    Sunday every 15 minutes from 00:00 to 11:59
    Monday every 0 minutes from 00:00 to 00:00
    Monday every 15 minutes from 00:00 to 11:59
    Tuesday every 0 minutes from 00:00 to 00:00
    Tuesday every 15 minutes from 00:00 to 11:59
    Wednesday every 0 minutes from 00:00 to 00:00
    Wednesday every 15 minutes from 00:00 to 11:59
    Thursday every 0 minutes from 00:00 to 00:00
    Thursday every 15 minutes from 00:00 to 11:59
    Friday every 0 minutes from 00:00 to 00:00
    Friday every 15 minutes from 00:00 to 11:59
    Saturday every 0 minutes from 00:00 to 00:00
    Saturday every 15 minutes from 00:00 to 11:59

    however, powershell seems to think Sunday is day zero, and Saturday is day 6. I strongly believe Monday to be day zero. my computer's regional/date settings agree with me wholeheartedly. how might I go about convincing powershell that Monday is the first day of the week?



    Monday, April 8, 2019 8:57 PM
  • Just apply the day string as a property of a custom object and sort the object or use a calculated property to create a temporary column that is sortable.

    Sunday is day zero.  You can change that in your profile culture and calendar settings.  You can also create a custom enum that is in any order you want.


    \_(ツ)_/


    • Edited by jrv Monday, April 8, 2019 9:01 PM
    Monday, April 8, 2019 9:00 PM
  •  
    $order = @('Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday')
    
    $sortedschedulearray = foreach($day in $order)
        {
        $scheduleArray | ? {$_ -match $day}
        } 
    

    Monday, April 8, 2019 10:32 PM
  •  
    $order = @('Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday')
    
    $sortedschedulearray = foreach($day in $order)
        {
        $scheduleArray | ? {$_ -match $day}
        } 

    Is this supposed to be a guess?  What does it do?

    Use an enum to get the order you want.  An array is not the way to do this.


    \_(ツ)_/

    Monday, April 8, 2019 11:11 PM
  • the code sorts an array of strings containing the names of the days of the week ($schedulearray) in chronological order ($order) from Monday to Sunday.

    and outputs the sorted array ($sortedschedulearray).

    Monday, April 8, 2019 11:43 PM
  • the code sorts an array of strings containing the names of the days of the week ($schedulearray) in chronological order ($order) from Monday to Sunday.

    and outputs the sorted array ($sortedschedulearray).

    Yes.  That is what I have shown you how to do.  You need to make arrangements to tag each line with its "ordinal".  Get it?  "Ordinal" means orderd.  You need to use the integer value of the day to set the order.


    \_(ツ)_/

    Monday, April 8, 2019 11:46 PM
  • Example:

    $arrayOfDays | 
        Select @{n='DayOfWeek';e={$_}},@{n='DayOrdinal';e={[int][dayofweek]$_}} | 
        Sort-Object DayOrdinal

    P.S. - Chronologically is, by default and convention, Sunday through Saturday.


    \_(ツ)_/


    • Edited by jrv Monday, April 8, 2019 11:52 PM
    Monday, April 8, 2019 11:50 PM
  • right, except that the integer value doesn't meet the "Monday through Sunday" requirement if the integer value of Sunday is 0 and Saturday is 6.

    I found the array solution here

    https://stackoverflow.com/questions/48701384/powershell-sorting-in-custom-order

    and this code does what I need it to do, without changing any culture or calendar settings:

    $order = @('Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday')
    
    $sortedschedulearray = foreach($day in $order)
        {
        $scheduleArray | ? {$_ -match $day}
        } 

    Tuesday, April 9, 2019 12:10 AM
  • is this the correct enum solution? it seems to work as well, but doesn't seem all that much more streamlined than using the array.

     enum order
     {
     Monday = 0
     Tuesday = 1
     Wednesday = 2
     Thursday = 3
     Friday = 4
     Saturday = 5
     Sunday = 6
     }
    $schedulearray | sort -property @{Expression={[order]::$_}}

    Tuesday, April 9, 2019 12:30 AM
  • That will work but it only needs to  be like this:

    enum order{
        Monday
        Tuesday
        Wednesday
        Thursday
        Friday
        Saturday
        Sunday
    }
    

    The default is the index and only needs an explicit value if you want the value to be other than the ordinal.


    \_(ツ)_/

    Tuesday, April 9, 2019 1:46 AM
  • $list | sort-object { [dayofweek]$_ }

    Sunday
    Sunday
    Monday
    Monday
    Tuesday
    Wednesday
    Thursday
    Friday
    Friday
    Saturday

    • Edited by JS2010 Tuesday, April 9, 2019 3:17 AM
    Tuesday, April 9, 2019 3:14 AM
  • $list | sort-object { [dayofweek]$_ }

    Sunday
    Sunday
    Monday
    Monday
    Tuesday
    Wednesday
    Thursday
    Friday
    Friday
    Saturday

    You are about two hours behind in the conversation.


    \_(ツ)_/

    Tuesday, April 9, 2019 3:21 AM
  • Re chronologically "Monday is the first day of the week according to the international standard ISO 8601, but in the US, Canada, and Japan it's counted as the second day of the week."

    I'm of the opinion that those who consider Sunday the first day of the week should not refer to Saturday and Sunday as "the weekend." and should, at the very least, use "weekends," if those days are indeed the separate, opposite endS of a week, rather than the singular End unit of a week. Like "bookends." as in "how were your weekends?" and "what are you up to these weekends?" and "coming up next, the weather outlooks for your weekends," and "everybody's workin' for the weekends." 

    but that's just me. ;-) 

    Tuesday, April 9, 2019 4:04 AM
  • The ISO calendar is not the default for North America and has never been the default.  Don't ask anyone why.  The answers will be diverse and silly.

    If your culture is set to UK or and EU country than the enum should be correct ot you can use the "CultureInfo" type to get the order.

    [cultureinfo]::CurrentCulture.DateTimeFormat.DayNames


    \_(ツ)_/

    Tuesday, April 9, 2019 4:15 AM
  • Hi,

    Was your issue resolved?

    If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.

    If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.

    If no, please reply and tell us the current situation in order to provide further help.

    Best Regards,

    Lee


    Just do it.

    Tuesday, April 9, 2019 6:49 AM