I generated a text file with pseudo-random numbers like this:
-853340442 1130519212 -2070936922
-707168664 -2076185735 -2135012102
166464098 1928545126 5768715
1060168276 -684694617 395859713
-680897578 -2095893176 1457930442
299309402 192205833 1878010157
-678911642 2062673581 -1801057195
795693402 -631504846 2117889796
448959250 547707556 -1115929024
168558507 7468411 1600190097
-746131117 1557335455 73377787
-1144524558 2143073647 -2044347857
1862106004 -193937480 1596949168
-1193502513 -920620244 -365340967
-677065994 500654963 1031304603
Now I try to put it in order using linux sort
command:
sort prng >prngsorted
The result is not what I expected:
1060168276 -684694617 395859713
-1144524558 2143073647 -2044347857
-1193502513 -920620244 -365340967
166464098 1928545126 5768715
168558507 7468411 1600190097
1862106004 -193937480 1596949168
299309402 192205833 1878010157
448959250 547707556 -1115929024
-677065994 500654963 1031304603
-678911642 2062673581 -1801057195
-680897578 -2095893176 1457930442
-707168664 -2076185735 -2135012102
-746131117 1557335455 73377787
795693402 -631504846 2117889796
-853340442 1130519212 -2070936922
Obviously, sort
tries to parse strings and extract numbers for sorting. And it seems to ignore minus signs.
Is it possible to force sort
to be a bit dumber and just compare lines lexicographically? The result should be like this:
-1144524558 2143073647 -2044347857
-1193502513 -920620244 -365340967
-677065994 500654963 1031304603
-678911642 2062673581 -1801057195
-680897578 -2095893176 1457930442
-707168664 -2076185735 -2135012102
-746131117 1557335455 73377787
-853340442 1130519212 -2070936922
1060168276 -684694617 395859713
166464098 1928545126 5768715
168558507 7468411 1600190097
1862106004 -193937480 1596949168
299309402 192205833 1878010157
448959250 547707556 -1115929024
795693402 -631504846 2117889796
Note: I tried -d
option but it did not help
Note 2: Probably I should use another utility instead of sort
?
Sorting words in lexicographical order mean that we want to arrange them first by the first letter of the word. Then for the words whose first letter is the same, we arrange them within that group by the second letter and so on just like in a language's dictionary(not the data structure).
sorted() function sorts data elements in lexicographical order by replicating the input list and keeping the input list as it is. Strings in Python can be sorted by initially splitting and applying sort.
How to sort by number. To sort by number pass the -n option to sort . This will sort from lowest number to highest number and write the result to standard output. Suppose a file exists with a list of items of clothing that has a number at the start of the line and needs to be sorted numerically.
-k Option: Unix provides the feature of sorting a table on the basis of any column number by using -k option. Use the -k option to sort on a certain column. For example, use “-k 2” to sort on the second column.
The sort
command takes account of your locale settings. Many of the locales ignore dashes for collation.
You can get appropriate sorting with
LC_COLLATE=C sort filename
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With