Tcl: Lsort output differs from bash sort

From FVue
Jump to: navigation, search

Problem

Bash sort sorts different compared to tcl lsort. E.g. bash outputs:

$ { echo a-b; echo ab; echo a-; } | sort
a-
ab
a-b
$

Whereas tcl outputs:

% set a {a-b a- ab}
a-b a- ab
% join [lsort $a] "\n"
a-
a-b
ab
%

The difference happens because both environments use a different sorting order:

  • bash sort uses the current locale's collating sequence and character set
  • tcl lsort uses a C-like sort order where each character is sorted according to its numeric value

Solution 1: Let bash sort like tcl lsort

$ export LC_COLLATE=C
$ { echo a-b; echo ab; echo a-; } | sort
a-
a-b
ab
$ unset LC_COLLATE
$

Solution 2: Let tcl sort like bash sort

% set a {a-b a- ab}
a-b a- ab
% exec sort << [join $a "\n"]
a-
ab
a-b
%

Comments

blog comments powered by Disqus