What Sort Order Does Linux Use

What sort order does Linux use?

The sort order for many commands (incl. bash glob, ls, sort) is based on your current locale settings.

You can force the collation by setting the LC_COLLATE environment variable. Setting it to C will perform a comparison on byte values.

On my system (en_US.utf8):

sh$ touch eleve
sh$ touch élève
sh$ touch Eleve
sh$ touch Élève
sh$ touch äkta
sh$ touch österreich

sh$ ls
äkta eleve Eleve élève Élève österreich pommes

sh$ LC_COLLATE=fr_FR.utf8 ls
äkta eleve Eleve élève Élève österreich pommes

sh$ LC_COLLATE=sv_SE.utf8 ls
eleve Eleve élève Élève pommes äkta österreich

sh$ LC_COLLATE=C ls
Eleve eleve pommes Élève äkta élève österreich

Linux sort by column and in reverse order

I just needed to remove the "n" next to column number:

sort -k 2 -r file.txt

Sub-sorting without losing original sort order?

You can try -V (sort by version):

find . -name '*.pdf' | cut -d'/' -f2 | sort -t _ -k2V

FOOBAR_1A.8_Alice.pdf
FOOBAR_1A.9_Bob.pdf
FOOBAR_1B.1_Jill.pdf
FOOBAR_1B.2_John.pdf
FOOBAR_1B.3_Mary.pdf
FOOBAR_1B.10_Foo.pdf
FOOBAR_1B.11_Bar.pdf
FOOBAR_1B.12_Jack.pdf

java File sorting order in windows and linux difference

You will need to implement your own Comparator<File> since the File.compareTo uses the "systems" order.

I think (not checked) that Linux uses the "standard" order by file name (case sensitive) so an example implementation could look like this:

public static void main(String[] args) {
List<File> files = new ArrayList<File>();
files.add(new File("test_1a.play"));
files.add(new File("test_1AA.play"));
files.add(new File("test_1aaa.play"));
files.add(new File("test-_1AAAA.play"));

Collections.sort(files, new Comparator<File>() {
@Override
public int compare(File o1, File o2) {
String p1 = o1.getAbsolutePath();
String p2 = o2.getAbsolutePath();
return p1.compareTo(p2);
}
});

System.out.println(files);
}

Outputs:

[test-_1AAAA.play, test_1AA.play, test_1a.play, test_1aaa.play]

Using awk and sort last column in descending order in Linux

First, assuming there are really not blank lined in between each line of data in data.csv, all you need is sort, you don't need awk at all. For example, since there is only ':' before the total score you want to sort descending by, you can use:

sort -t: -k2,2rn data.csv

Where -t: tells sort to use ':' as the field separator and then the KEYDEF -k2,2rn tell sort to use the 2nd field (what's after the ':' to sort by) and the rn says use a reverse numeric sort on that field.

Example Use/Output

With your data (without blank lines) in data.csv, you would have:

$ sort -t: -k2,2rn data.csv
2014,Honda,Accord,83 Total score:112
1998,Subaru,Legacy,23 Total score:62
2008,Honda,Accord,88 Total score:48
2012,Volkswagen,Golf,59 Total score:28
2016,Bmw,M2,2 Total score:24
2001,Dodge,Viper,42 Total score:8
2015,Chevy,Camaro,0 Total score:0

Which is your sort by Total score in descending order. If you want ascending order, just remove the r from -k2,2rn.

If you do have blank lines, you can remove them before the sort with sed -i '/^$/d' data.csv.

Sorting by number Before "Total score"

If you want to sort by the number that begins the XX Total score: yy field (e.g. XX), you can use sort with the field separator being a ',' and then your KEYDEF would be -k4.1,4.3rn which just says sort using the 4th field character 1 through character 3 by the same reverse numeric, e.g.

sort -t, -k4.1,4.3rn data.csv

Example Use/Output

In this case, sorting by the number before Total score in descending order would result in:

$ sort -t, -k4.1,4.3rn data.csv
2008,Honda,Accord,88 Total score:48
2014,Honda,Accord,83 Total score:112
2012,Volkswagen,Golf,59 Total score:28
2001,Dodge,Viper,42 Total score:8
1998,Subaru,Legacy,23 Total score:62
2016,Bmw,M2,2 Total score:24
2015,Chevy,Camaro,0 Total score:0

After posting the original solution I noticed it was ambiguous as which of the numbers on the 4th field you intended to sort on. In either case, here are both solutions. Let me know if you have further questions.

Is the order of iteration in bash for loop guaranteed?

According to the bash man page:

Pathname Expansion

After word splitting, unless the -f option has been set, bash scans
each word for the characters *, ?, and [. If one of these characters
appears, then the word is regarded as a pattern, and replaced with an
alphabetically sorted list of filenames matching the pattern (see
Pattern Matching below).

So, the files will be processed in alphabetical order (which depends on your locale, see comments) due to globbing expansion rules.



Related Topics



Leave a reply



Submit