Alex Morales
Alex Morales

Reputation: 2791

GNU sort - What is the default algorithm used for comparison?

I need help understanding the default algorithm for GNU's sort. I assumed it did a lexicographic sort, however I found out some behavior that does not correspond to that, as an example take the following strings:

alex.
alex.a
alex.Z
alexa
alex0
alexZ
alex.~
alex
alex.|
alex.}
alex.abc

And sort them on a shell using sort like echo 'stuff' | sort

This is the result I get:

alex
alex.
alex.~
alex.|
alex.}
alex0
alexa
alex.a
alex.abc
alexZ
alex.Z

And I can't figure out why alex0 and alexa appear in between alex.} and alex.a

Can someone explain this to me?

Upvotes: 3

Views: 3004

Answers (4)

Ian Chapman
Ian Chapman

Reputation: 1

Yes, I think I've figured this out. The "/" character seems to be ignored. I wonder where that detail is documented, maybe it is hidden in LC_ALL.

Upvotes: 0

Ian Chapman
Ian Chapman

Reputation: 1

Try "sort InFile.txt" on this file then change the first word Start to Begin & explain the difference? I keep setting LC_LOCAL-C to no effect. InFile.txt

Start report    /* Change Start to Begin to see difference */    
MISSING..
NEW file.
Updated files
/home/me/path/To/file.txt
/home/me/path/To/new.txt
/home/me/path/To/old.txt
/home/me/path/To/lost.txt
/home/me/path/To/file.txt

Upvotes: 0

tink
tink

Reputation: 15239

Sorting by and large depends on LOCALE settings:

$ sort sort 
alex
alex.
alex.~
alex.|
alex.}
alex0
alexa
alex.a
alex.abc
alexZ
alex.Z
$ LC_ALL=C sort sort
alex
alex.
alex.Z
alex.a
alex.abc
alex.|
alex.}
alex.~
alex0
alexZ
alexa

Upvotes: 4

Vikram
Vikram

Reputation: 7525

GNU Sort implements mergesort. You can find the complete answer posted in super user thread. https://superuser.com/questions/234054/unix-sort-algorithm-implementation

Upvotes: 4

Related Questions