lsort
Gives a sorted copy of a list.
lsort ?OPTION ...? LIST
Description
Gives the elements of LIST in sorted order, as a new list. The type options say how two elements compare: -ascii (the default) compares their text by the code points of the characters, -dictionary compares runs of digits as numbers, -integer and -real compare numbers, and -command calls a script. -increasing (the default) and -decreasing give the direction.
The sort is stable: elements that compare equal keep their order. -unique keeps one element of each group of equal elements, -indices gives indexes in place of elements, -index compares an element of each element, and -stride sorts groups of elements.
A unique prefix of an option is the option, and the options come before LIST. An empty LIST gives an empty list.
Arguments
| Argument | Type | Default | Description |
|---|---|---|---|
| LIST | list | required | The list to sort. |
Options
| Option | Type | Default | Summary |
|---|---|---|---|
-ascii | flag | none | Compares the elements as text; the default type. |
-dictionary | flag | none | Compares the elements in dictionary order: no case, and runs of digits as numbers. |
-integer | flag | none | Compares the elements as integers. |
-real | flag | none | Compares the elements as floating-point numbers. |
-command | command prefix | none | Compares two elements with a command. |
-increasing | flag | none | Sorts from the smallest element to the largest; the default. |
-decreasing | flag | none | Sorts from the largest element to the smallest. |
-nocase | flag | none | Compares the text with no regard to case. |
-unique | flag | none | Keeps one element of each group of equal elements. |
-indices | flag | none | Gives the indexes of the elements in sorted order. |
-index | index, or a list of indexes | none | Compares an element of each element in place of the whole element. |
-stride | integer, 2 or more | none | Sorts groups of N elements. |
-ascii
Compares the elements as text; the default type.
Compares the text of two elements by the code point of each character, so each uppercase ASCII letter comes before each lowercase one, and 10 comes before 9.
% lsort -ascii {b B a A}
A B a b
% lsort {10 9 100}
10 100 9Each value has a text, so each list sorts.
-dictionary
Compares the elements in dictionary order: no case, and runs of digits as numbers.
Compares the text of two elements with no regard to case, and a run of digits as an integer, so a2 comes before a10. When two elements are the same with no regard to case, an uppercase letter comes before its lowercase one.
% lsort -dictionary {a10 a2 A1}
A1 a2 a10
% lsort -dictionary {b B a A}
A a B b
% lsort -dictionary {x1.5 x1.10}
x1.5 x1.10Each value has a text, so each list sorts.
-integer
Compares the elements as integers.
Each element must be an integer, in any form of Tcl, such as 0x10. Equal integers in a different form, such as 1 and 01, compare as equal.
% lsort -integer {10 9 100}
9 10 100
% lsort -integer {0x10 9}
9 0x10% lsort -integer {10 x}
expected integer but got "x"
-errorcode NONE-real
Compares the elements as floating-point numbers.
Each element must be a number. The elements keep their form in the result: 1e2 stays 1e2.
% lsort -real {1.5 1e1 -2}
-2 1.5 1e1
% lsort -real {1e2 99.5}
99.5 1e2% lsort -real {1 x}
expected floating-point number but got "x"
-errorcode NONE-command SCRIPT
Compares two elements with a command.
Calls SCRIPT with two elements added as arguments. Its result must be an integer: negative when the first element comes first, 0 when they are equal, and positive when the second comes first. An error of the command stops the sort with that error.
% proc cmp {a b} {expr {[string length $a] - [string length $b]}}
> lsort -command cmp {ccc a bb}
a bb ccc
% lsort -command {apply {{a b} {expr {$b - $a}}}} {1 3 2}
3 2 1% proc bad {a b} {return x}
> lsort -command bad {a b}
-compare command returned non-integer result
-errorcode NONE
% lsort -command nocmd {a b}
invalid command name "nocmd"
-errorcode TCL LOOKUP COMMAND nocmd-increasing
Sorts from the smallest element to the largest; the default.
Puts the element that compares smallest first.
% lsort -increasing {a c b}
a b cA flag has no value to check.
-decreasing
Sorts from the largest element to the smallest.
Puts the element that compares largest first. Equal elements keep their order also in this direction.
% lsort -decreasing {a c b}
c b a
% lsort -indices -decreasing {c a b}
0 2 1A flag has no value to check.
-nocase
Compares the text with no regard to case.
With -ascii (the default), compares the letters as lowercase. Elements that are equal with no regard to case keep their order.
% lsort -nocase {b B a A}
a A b B
% lsort -unique -nocase {b B a}
a BA flag has no value to check.
-unique
Keeps one element of each group of equal elements.
Of each group of elements that compare equal, the result keeps the last one in LIST. With -integer, 1 and 01 are equal, so one of them goes away.
% lsort -unique {b a b a}
a b
% lsort -integer -unique {1 01 2}
01 2
% lsort -indices -unique {b a b}
1 2A flag has no value to check.
-indices
Gives the indexes of the elements in sorted order.
Gives the index in LIST of each element of the sorted result, in place of the element.
% lsort -indices {c a b}
1 2 0A flag has no value to check.
-index INDEX
Compares an element of each element in place of the whole element.
Each element of LIST is a list, and the sort compares its element at INDEX, or at the path of a list of indexes. Each element must have that index. The result has the whole elements.
% lsort -index 1 -decreasing {{a 1} {b 3} {c 2}}
{b 3} {c 2} {a 1}
% lsort -index end {{a 1} {b 0}}
{b 0} {a 1}
% lsort -index {1 0} {{a {z 1}} {b {y 2}}}
{b {y 2}} {a {z 1}}% lsort -index 1 {{a 1} {b}}
element 1 missing from sublist "b"
-errorcode NONE-stride N
Sorts groups of N elements.
The list is groups of N elements, and its length must be a multiple of N. The sort compares the first element of each group, or the element of -index in the group, and moves each group whole.
% lsort -stride 2 {b 2 a 1}
a 1 b 2
% lsort -stride 2 -index 1 {b 2 a 1}
a 1 b 2% lsort -stride 3 {a b}
list size must be a multiple of the stride length
-errorcode NONE
% lsort -stride 1 {b a}
stride length must be at least 2
-errorcode NONE
% lsort -stride 2 -index 2 {a b}
when used with "-stride", the leading "-index" value must be within the group
-errorcode NONEReturns
The sorted list, or the list of the indexes of the elements in sorted order with -indices.
Examples
% lsort {pear apple fig}
apple fig pear
# The text of the elements, by default.
% lsort {10 9 100}
10 100 9
% lsort -integer {10 9 100}
9 10 100
% lsort -dictionary {a10 a2 A1}
A1 a2 a10
% lsort -index 1 -decreasing {{a 1} {b 3} {c 2}}
{b 3} {c 2} {a 1}
Errors
% lsort
wrong # args: should be "lsort ?-option value ...? list"
-errorcode TCL WRONGARGS
% lsort -bogus {a}
bad option "-bogus": must be -ascii, -command, -decreasing, -dictionary, -increasing, -index, -indices, -integer, -nocase, -real, -stride, or -unique
-errorcode NONE
% lsort "a {b"
unmatched open brace in list
-errorcode TCL VALUE LIST BRACE
Differences from Tcl
- Most errors have
-errorcode NONE, where Tcl 8.6.16 and 9.0.4 give a code such asTCL LOOKUP INDEX optionorTCL OPERATION LSORT INDEXFAILED(issue 658).
Facts
- Capability
- None.
- Tcl release
- Tcl 9.1 and 8.6.
- Since
- ztcl 1.0.0-beta.1