# Sorting a string that has date/time

**URL:** <https://forums.powershell.org/t/sorting-a-string-that-has-date-time/7782>\
**Category:** PowerShell Help\
**Created:** [January 2, 2017, 8:16pm UTC](https://forums.powershell.org/t/sorting-a-string-that-has-date-time/7782 "2017-01-02T20:16:38Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![scott-windmiller](https://avatars.discourse-cdn.com/v4/letter/s/c4cdca/32.png) [@scott-windmiller](https://forums.powershell.org/u/scott-windmiller)\
**Post date:** [January 2, 2017, 8:16pm UTC](https://forums.powershell.org/t/sorting-a-string-that-has-date-time/7782/1 "2017-01-02T20:16:38Z")

</div>

I have a bunch of Dates and times that I think are just strings.

```
$data | Select DateAdded
5/20/2015 8:27:34 AM
7/13/2016 8:10:58 AM
6/04/2012 12:10:58 PM
```

Wanting to sort would give me:

```
$data | Select DateAdded | Sort DateAdded
5/20/2015 8:27:34 AM
6/04/2012 12:10:58 PM
7/13/2016 8:10:58 AM
```

But what I want is:

```
6/04/2012 12:10:58 PM
5/20/2015 8:27:34 AM
7/13/2016 8:10:58 AM
```

How can I get it to sort using the Year Month Day Time?

Thanks!  
Scott

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [January 2, 2017, 8:24pm UTC](https://forums.powershell.org/t/sorting-a-string-that-has-date-time/7782/2 "2017-01-02T20:24:25Z")

</div>

```
$data | Select-Object -Property @{Name='Date';Expression={Get-Date $_.DateAdded}} | Sort-Object -Property Date
```

---

<div class="post-metadata">

**Author:** ![scott-windmiller](https://avatars.discourse-cdn.com/v4/letter/s/c4cdca/32.png) [@scott-windmiller](https://forums.powershell.org/u/scott-windmiller)\
**Post date:** [January 3, 2017, 9:48am UTC](https://forums.powershell.org/t/sorting-a-string-that-has-date-time/7782/3 "2017-01-03T09:48:18Z")

</div>

Thanks, this worked great. I think my hangup was thinking the Get-Date was only to get the current date/time and not actually setting something as a date/time object.

Thanks for the help!

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:39pm UTC](https://forums.powershell.org/t/sorting-a-string-that-has-date-time/7782/4 "2024-05-16T20:39:41Z")

</div>

Closed on May 16, 2024, 8:39pm UTC


