I am working with a powershell script where I have an array containing following data. Each row below is an element in the array.
Each element has two parts seperated by "/" a number (can be 1-12 digits) and a date time.
I need to sort the array according to the date and time given in each element.
201410212339/21-Oct-2014 23:50 -
2251/27-Sep-2014 23:02 -
0436/22-Oct-2014 04:47 -
091342/09-Oct-2014 13:53 -
2220743/22-Oct-2014 07:53 -
20140/22-Sep-2014 07:41 -
2190446/19-Oct-2014 04:56 -
2014258/21-Aug-2014 23:21 -
22110/22-Oct-2014 14:21 -
1410221721/22-Jun-2014 17:33 -
130/23-Jul-2014 11:42 -
10231426/23-Feb-2014 14:38 -
231731/23-Jan-2014 17:43 -
0232039/23-Mar-2014 20:51 -
Can anyone help me with this? I want to sort the array to access the latest or the second latest entry and use the number associated with it. I can try to split each element into number and date-time and sort them but I am looking for a much simpler way.
Thanks in advance.
If you want to sort by date and time, this is one way of doing it.
This is your data as an array
Strip extra characters from the end of each line
Pre-pend each line by [datetime] representation of date in ticks
Sort
Remove pre-pended date string and restore the garbage on the end if you want.
Here is the result:
EDIT:
My initial attempt at sorting by [datetime] did not work properly
This not suitable for sorting, as it does not sort by year or time. Representing [datetime] in ticks fixes the problem.
I have edited the code above to reflect this.