Getting a Sort to work on a Date Object (but after converting it?)

Welcome Forums General PowerShell Q&A Getting a Sort to work on a Date Object (but after converting it?)

Viewing 2 reply threads
  • Author
    Posts
    • #227821
      Participant
      Topics: 8
      Replies: 15
      Points: 76
      Rank: Member

      Hi Folks,

      Having a trouble getting my date object to sort correctly.

      I have a very simple script that will just list the certificates on a server. But, the expiry date is US format and I want to force it to European date format and sort so that those expiring are listed first.

      So, if I run the script as follows, it sorts correctly – on the US date format

      And it outputs as follows, and the sort works.

      I’ve tried a few ways to get this part into dd/mm/yyyy format:

      so, I played around with different variations of the following:

      Thinking that it would pipe the original format into Get-Date to the Uformat – and then sort it as a Real Date, but it doesn’t – it sorts as a string, and just sorts on the day value like ascending integers.

      I guess I don’t want to convert the date to UK format, I just want to format it as UK format and sort correctly…It’s driving me nuts, I’m sure it’s a simple answer for sure… and maybe Olaf will come to my rescue? 😉

      Thanks, Alex74

       

       

    • #227836
      Participant
      Topics: 17
      Replies: 1872
      Points: 3,640
      Helping Hand
      Rank: Community Hero

      Sorting dates either needs to be a DateTime object or a sortable date. Sorting strings like that can have some odd behavior. Here is another way to do it that should be a bit cleaner:

    • #227905
      Participant
      Topics: 8
      Replies: 15
      Points: 76
      Rank: Member

      Rob, you are a squire and a gentleman, thanks for the explanation and the solution – works just great!

Viewing 2 reply threads
  • The topic ‘Getting a Sort to work on a Date Object (but after converting it?)’ is closed to new replies.