I have a large array (100k+ lines) that shall be sorted. The sort criteria is a time stamp in the lines. The naive approach to use the sort function doesn't work. How can I sort it by date/time?
The array is a file content. But this examples shows how to make it wrong:
$a = @()
$a += "Line SystemTime LocalTime Facility Severity ID Message"
$a += "0 1/10/2019 14:30:30,639 1/9/2019 15:30:30,639 Data base E ID:03590006 Init - Could not open file (lastError=0x00000037)"
$a += "2 1/9/2019 14:30:40,442 1/9/2019 15:30:40,442 FTP W ID:035A001A select failed, err:10038. Shutdown FTP"
$a += "5 1/9/2019 14:30:30,639 1/9/2019 15:30:30,639 Data base E ID:03590006 Could not connect to changed event."
$a += "3 1/9/2019 14:30:40,460 1/9/2019 15:30:40,460 Telnet W ID:02FE000E select failed, err:10038. Shutdown Telnet"
$a | sort
The line number in front of the time stamp shall be ignored. Obviously the sort cmdlet sorts alphabetically. How can I tell it that it should use the date/time?
If your data is CSV, your best bet would be to convert it to using the ConvertFrom-CSV
cmdlet and sort it by the desired property.
In your example, you could use a regular expression within the Sort-Object
cmdlet to grab e. g. the first date and cast it to a datetime:
$a | Select-Object -Skip 1 |
Sort-Object { [datetime]($_ -replace '^\d*\s*([^,]+).*', '$1')}
Note: I had to skip the header (first line)
Output:
5 1/9/2019 14:30:30,639 1/9/2019 15:30:30,639 Data base E ID:03590006 Could not connect to changed event.
2 1/9/2019 14:30:40,442 1/9/2019 15:30:40,442 FTP W ID:035A001A select failed, err:10038. Shutdown FTP
3 1/9/2019 14:30:40,460 1/9/2019 15:30:40,460 Telnet W ID:02FE000E select failed, err:10038. Shutdown Telnet
0 1/10/2019 14:30:30,639 1/9/2019 15:30:30,639 Data base E ID:03590006 Init - Could not open file (lastError=0x00000037)
User contributions licensed under CC BY-SA 3.0