4wk_
4wk_

Reputation: 2743

Bash: how does 'sort' sort paths?

How does sort work? I have this file:

/test# cat foobar
html/lib/ORM/aaa.php
html/lib/ORMBase/ormbase_aaa.php
html/lib/ORM/zzz.php
html/lib/ORMBase/ormbase_zzz.php

And this is the output of sort:

/test# cat foobar | sort
html/lib/ORM/aaa.php
html/lib/ORMBase/ormbase_aaa.php
html/lib/ORMBase/ormbase_zzz.php
html/lib/ORM/zzz.php

I tried a lot of options: -f, -i, -t/... and I dont get it. I want to understand why sort thinks this is sorted.


NB: It works fine with this other sample:

/test# cat foobar2
a/a/a
a/ab/a
a/ab/b
a/a/ab
a/abc/a

/test# cat foobar2 | sort
a/a/a
a/a/ab
a/ab/a
a/ab/b
a/abc/a

Upvotes: 5

Views: 2009

Answers (1)

Boldewyn
Boldewyn

Reputation: 82784

sort tries to be clever with regard to localization. It ignores some non-alphanumeric characters like / and so on. The man page has a short sentence on that:

* WARNING * The locale specified by the environment affects sort order. Set LC_ALL=C to get the traditional sort order that uses native byte values.

So, to fix your issue:

$ cat foobar | LC_ALL=C sort

Upvotes: 9

Related Questions