'How to sort strings in a file numerically

In my batch script there will be a temporary file with the notes that are in a section (gotten from a json file but that doesn't matter)

Format: (position of note in milliseconds),(kind of note),(if not 0 the note is supposed to be held and it's the time milliseconds the note is supposed to be held)

(the format is not important)

but some of the time it's not sorted

Example:

2285.71428571429,0,0
2367.85714285714,0,0
2455.35714285714,0,0
2576.78571428571,1,0
2757.14285714286,1,0
3046.42857142857,0,0
2962.5,1,0
3128.57142857143,0,0
3216.07142857143,0,0
3298.21428571429,1,0
3380.35714285714,1,0
2669.64285714286,3,0
2882.14285714286,3,0


So I would like to sort them but the sort command cannot sort them properly

Expected results:

2285.71428571429,0,0
2367.85714285714,0,0
2455.35714285714,0,0
2576.78571428571,1,0
2669.64285714286,3,0
2757.14285714286,1,0
2882.14285714286,3,0
2962.5,1,0
3046.42857142857,0,0
3128.57142857143,0,0
3216.07142857143,0,0
3298.21428571429,1,0
3380.35714285714,1,0

The line items does not need sorting. The line needs to be sorted.



Solution 1:[1]

You state, the Format is not important So I opted for table output. Seeing as your input seems to be csv format, we can use powershell's Import-Csv fucntion to pull in the data delimit on , and then use Sort-Object to sort decimal numbers accordingly.

Here we are setting headers for Col1 to Col3 (as per your example), then sorting on Col1, with the remainder of the columns in tact.

$file = Import-Csv -Header "Col1", "Col2", "Col3" -delimiter ',' test.txt
$file | Sort-Object { [double]$_.Col1 } | Format-Table -AutoSize -Wrap

Result:

PS Z:\> $file | Sort-Object -Descending { [double]$_.Col1 } | Format-Table -AutoSize -Wrap

Col1             Col2 Col3
----             ---- ----
2285.71428571429 0    0
2367.85714285714 0    0
2455.35714285714 0    0
2576.78571428571 1    0
2669.64285714286 3    0
2757.14285714286 1    0
2882.14285714286 3    0
2962.5           1    0
3046.42857142857 0    0
3128.57142857143 0    0
3216.07142857143 0    0
3298.21428571429 1    0
3380.35714285714 1    0

You can run directly from cmd as:

powershell "$file = Import-Csv -Header "Col1", "Col2", "Col3" -delimiter ',' test.txt; $file | Sort-Object { [double]$_.Col1 } | Format-Table -AutoSize -Wrap"

Result:

PS Z:\> $file | Sort-Object -Descending { [double]$_.Col1 } | Format-Table -AutoSize -Wrap

Col1             Col2 Col3
----             ---- ----
2285.71428571429 0    0
2367.85714285714 0    0
2455.35714285714 0    0
2576.78571428571 1    0
2669.64285714286 3    0
2757.14285714286 1    0
2882.14285714286 3    0
2962.5           1    0
3046.42857142857 0    0
3128.57142857143 0    0
3216.07142857143 0    0
3298.21428571429 1    0
3380.35714285714 1    0

Or hybrid into batch-file and joining variables with , to give you the same output result as input:

@for /F "skip=3 tokens=1-3" %%i in ('powershell "$file = Import-Csv -Header "Col1", "Col2", "Col3" -delimiter ',' test.txt; $file | Sort-Object { [double]$_.Col1 }"') do @echo(%%i,%%j,%%k

Result:

Z:\>sort_object-Int.cmd
2285.71428571429,0,0
2367.85714285714,0,0
2455.35714285714,0,0
2576.78571428571,1,0
2669.64285714286,3,0
2757.14285714286,1,0
2882.14285714286,3,0
2962.5,1,0
3046.42857142857,0,0
3128.57142857143,0,0
3216.07142857143,0,0
3298.21428571429,1,0
3380.35714285714,1,0

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1