| 1 |
'\" |
| 2 |
'\" Copyright (c) 1993 The Regents of the University of California. |
| 3 |
'\" Copyright (c) 1994-1996 Sun Microsystems, Inc. |
| 4 |
'\" Copyright (c) 1999 Scriptics Corporation |
| 5 |
'\" Copyright (c) 2001 Kevin B. Kenny <[email protected]>. All rights reserved. |
| 6 |
'\" |
| 7 |
'\" See the file "license.terms" for information on usage and redistribution |
| 8 |
'\" of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
| 9 |
'\" |
| 10 |
.TH lsort n 8.5 Tcl "Tcl Built-In Commands" |
| 11 |
.so man.macros |
| 12 |
.BS |
| 13 |
'\" Note: do not modify the .SH NAME line immediately below! |
| 14 |
.SH NAME |
| 15 |
lsort \- Sort the elements of a list |
| 16 |
.SH SYNOPSIS |
| 17 |
\fBlsort \fR?\fIoptions\fR? \fIlist\fR |
| 18 |
.BE |
| 19 |
.SH DESCRIPTION |
| 20 |
.PP |
| 21 |
This command sorts the elements of \fIlist\fR, returning a new |
| 22 |
list in sorted order. The implementation of the \fBlsort\fR command |
| 23 |
uses the merge\-sort algorithm which is a stable sort that has O(n log |
| 24 |
n) performance characteristics. |
| 25 |
.PP |
| 26 |
By default ASCII sorting is used with the result returned in |
| 27 |
increasing order. However, any of the following options may be |
| 28 |
specified before \fIlist\fR to control the sorting process (unique |
| 29 |
abbreviations are accepted): |
| 30 |
.TP |
| 31 |
\fB\-ascii\fR |
| 32 |
. |
| 33 |
Use string comparison with Unicode code-point collation order (the |
| 34 |
name is for backward-compatibility reasons.) This is the default. |
| 35 |
.TP |
| 36 |
\fB\-dictionary\fR |
| 37 |
. |
| 38 |
Use dictionary-style comparison. This is the same as \fB\-ascii\fR |
| 39 |
except (a) case is ignored except as a tie-breaker and (b) if two |
| 40 |
strings contain embedded numbers, the numbers compare as integers, |
| 41 |
not characters. For example, in \fB\-dictionary\fR mode, \fBbigBoy\fR |
| 42 |
sorts between \fBbigbang\fR and \fBbigboy\fR, and \fBx10y\fR |
| 43 |
sorts between \fBx9y\fR and \fBx11y\fR. Overrides the \fB\-nocase\fR |
| 44 |
option. |
| 45 |
.TP |
| 46 |
\fB\-integer\fR |
| 47 |
. |
| 48 |
Convert list elements to integers and use integer comparison. |
| 49 |
.TP |
| 50 |
\fB\-real\fR |
| 51 |
. |
| 52 |
Convert list elements to floating-point values and use floating comparison. |
| 53 |
.TP |
| 54 |
\fB\-command\0\fIcommand\fR |
| 55 |
. |
| 56 |
Use \fIcommand\fR as a comparison command. |
| 57 |
To compare two elements, evaluate a Tcl script consisting of |
| 58 |
\fIcommand\fR with the two elements appended as additional |
| 59 |
arguments. The script should return an integer less than, |
| 60 |
equal to, or greater than zero if the first element is to |
| 61 |
be considered less than, equal to, or greater than the second, |
| 62 |
respectively. |
| 63 |
.TP |
| 64 |
\fB\-increasing\fR |
| 65 |
. |
| 66 |
Sort the list in increasing order |
| 67 |
.PQ smallest "items first" . |
| 68 |
This is the default. |
| 69 |
.TP |
| 70 |
\fB\-decreasing\fR |
| 71 |
. |
| 72 |
Sort the list in decreasing order |
| 73 |
.PQ largest "items first" . |
| 74 |
.TP |
| 75 |
\fB\-indices\fR |
| 76 |
. |
| 77 |
Return a list of indices into \fIlist\fR in sorted order instead of |
| 78 |
the values themselves. |
| 79 |
.TP |
| 80 |
\fB\-index\0\fIindexList\fR |
| 81 |
. |
| 82 |
If this option is specified, each of the elements of \fIlist\fR must |
| 83 |
itself be a proper Tcl sublist (unless \fB\-stride\fR is used). |
| 84 |
Instead of sorting based on whole sublists, \fBlsort\fR will extract |
| 85 |
the \fIindexList\fR'th element from each sublist (as if the overall |
| 86 |
element and the \fIindexList\fR were passed to \fBlindex\fR) and sort |
| 87 |
based on the given element. |
| 88 |
For example, |
| 89 |
.RS |
| 90 |
.PP |
| 91 |
.CS |
| 92 |
\fBlsort\fR -integer -index 1 \e |
| 93 |
{{First 24} {Second 18} {Third 30}} |
| 94 |
.CE |
| 95 |
.PP |
| 96 |
returns \fB{Second 18} {First 24} {Third 30}\fR, |
| 97 |
.PP |
| 98 |
'\" |
| 99 |
'\" This example is from the test suite! |
| 100 |
'\" |
| 101 |
.CS |
| 102 |
\fBlsort\fR -index end-1 \e |
| 103 |
{{a 1 e i} {b 2 3 f g} {c 4 5 6 d h}} |
| 104 |
.CE |
| 105 |
.PP |
| 106 |
returns \fB{c 4 5 6 d h} {a 1 e i} {b 2 3 f g}\fR, |
| 107 |
and |
| 108 |
.PP |
| 109 |
.CS |
| 110 |
\fBlsort\fR -index {0 1} { |
| 111 |
{{b i g} 12345} |
| 112 |
{{d e m o} 34512} |
| 113 |
{{c o d e} 54321} |
| 114 |
} |
| 115 |
.CE |
| 116 |
.PP |
| 117 |
returns \fB{{d e m o} 34512} {{b i g} 12345} {{c o d e} 54321}\fR |
| 118 |
(because \fBe\fR sorts before \fBi\fR which sorts before \fBo\fR.) |
| 119 |
This option is much more efficient than using \fB\-command\fR |
| 120 |
to achieve the same effect. |
| 121 |
.RE |
| 122 |
.TP |
| 123 |
\fB\-stride\0\fIstrideLength\fR |
| 124 |
. |
| 125 |
If this option is specified, the list is treated as consisting of |
| 126 |
groups of \fIstrideLength\fR elements and the groups are sorted by |
| 127 |
either their first element or, if the \fB\-index\fR option is used, |
| 128 |
by the element within each group given by the first index passed to |
| 129 |
\fB\-index\fR (which is then ignored by \fB\-index\fR). Elements |
| 130 |
always remain in the same position within their group. |
| 131 |
.RS |
| 132 |
.PP |
| 133 |
The list length must be an integer multiple of \fIstrideLength\fR, which |
| 134 |
in turn must be at least 2. |
| 135 |
.PP |
| 136 |
For example, |
| 137 |
.PP |
| 138 |
.CS |
| 139 |
\fBlsort\fR \-stride 2 {carrot 10 apple 50 banana 25} |
| 140 |
.CE |
| 141 |
.PP |
| 142 |
returns |
| 143 |
.QW "apple 50 banana 25 carrot 10" , |
| 144 |
and |
| 145 |
.PP |
| 146 |
.CS |
| 147 |
\fBlsort\fR \-stride 2 \-index 1 \-integer {carrot 10 apple 50 banana 25} |
| 148 |
.CE |
| 149 |
.PP |
| 150 |
returns |
| 151 |
.QW "carrot 10 banana 25 apple 50" . |
| 152 |
.RE |
| 153 |
.TP |
| 154 |
\fB\-nocase\fR |
| 155 |
. |
| 156 |
Causes comparisons to be handled in a case-insensitive manner. Has no |
| 157 |
effect if combined with the \fB\-dictionary\fR, \fB\-integer\fR, or |
| 158 |
\fB\-real\fR options. |
| 159 |
.TP |
| 160 |
\fB\-unique\fR |
| 161 |
. |
| 162 |
If this option is specified, then only the last set of duplicate |
| 163 |
elements found in the list will be retained. Note that duplicates are |
| 164 |
determined relative to the comparison used in the sort. Thus if |
| 165 |
\fB\-index 0\fR is used, \fB{1 a}\fR and \fB{1 b}\fR would be |
| 166 |
considered duplicates and only the second element, \fB{1 b}\fR, would |
| 167 |
be retained. |
| 168 |
.SH "NOTES" |
| 169 |
.PP |
| 170 |
The options to \fBlsort\fR only control what sort of comparison is |
| 171 |
used, and do not necessarily constrain what the values themselves |
| 172 |
actually are. This distinction is only noticeable when the list to be |
| 173 |
sorted has fewer than two elements. |
| 174 |
.PP |
| 175 |
The \fBlsort\fR command is reentrant, meaning it is safe to use as |
| 176 |
part of the implementation of a command used in the \fB\-command\fR |
| 177 |
option. |
| 178 |
.SH "EXAMPLES" |
| 179 |
.PP |
| 180 |
Sorting a list using ASCII sorting: |
| 181 |
.PP |
| 182 |
.CS |
| 183 |
\fI%\fR \fBlsort\fR {a10 B2 b1 a1 a2} |
| 184 |
B2 a1 a10 a2 b1 |
| 185 |
.CE |
| 186 |
.PP |
| 187 |
Sorting a list using Dictionary sorting: |
| 188 |
.PP |
| 189 |
.CS |
| 190 |
\fI%\fR \fBlsort\fR -dictionary {a10 B2 b1 a1 a2} |
| 191 |
a1 a2 a10 b1 B2 |
| 192 |
.CE |
| 193 |
.PP |
| 194 |
Sorting lists of integers: |
| 195 |
.PP |
| 196 |
.CS |
| 197 |
\fI%\fR \fBlsort\fR -integer {5 3 1 2 11 4} |
| 198 |
1 2 3 4 5 11 |
| 199 |
\fI%\fR \fBlsort\fR -integer {1 2 0x5 7 0 4 -1} |
| 200 |
-1 0 1 2 4 0x5 7 |
| 201 |
.CE |
| 202 |
.PP |
| 203 |
Sorting lists of floating-point numbers: |
| 204 |
.PP |
| 205 |
.CS |
| 206 |
\fI%\fR \fBlsort\fR -real {5 3 1 2 11 4} |
| 207 |
1 2 3 4 5 11 |
| 208 |
\fI%\fR \fBlsort\fR -real {.5 0.07e1 0.4 6e-1} |
| 209 |
0.4 .5 6e-1 0.07e1 |
| 210 |
.CE |
| 211 |
.PP |
| 212 |
Sorting using indices: |
| 213 |
.PP |
| 214 |
.CS |
| 215 |
\fI%\fR # Note the space character before the c |
| 216 |
\fI%\fR \fBlsort\fR {{a 5} { c 3} {b 4} {e 1} {d 2}} |
| 217 |
{ c 3} {a 5} {b 4} {d 2} {e 1} |
| 218 |
\fI%\fR \fBlsort\fR -index 0 {{a 5} { c 3} {b 4} {e 1} {d 2}} |
| 219 |
{a 5} {b 4} { c 3} {d 2} {e 1} |
| 220 |
\fI%\fR \fBlsort\fR -index 1 {{a 5} { c 3} {b 4} {e 1} {d 2}} |
| 221 |
{e 1} {d 2} { c 3} {b 4} {a 5} |
| 222 |
.CE |
| 223 |
.PP |
| 224 |
.VS 8.6 |
| 225 |
Sorting a dictionary: |
| 226 |
.PP |
| 227 |
.CS |
| 228 |
\fI%\fR set d [dict create c d a b h i f g c e] |
| 229 |
c e a b h i f g |
| 230 |
\fI%\fR \fBlsort\fR -stride 2 $d |
| 231 |
a b c e f g h i |
| 232 |
.CE |
| 233 |
.PP |
| 234 |
Sorting using striding and multiple indices: |
| 235 |
.PP |
| 236 |
.CS |
| 237 |
\fI%\fR # Note the first index value is relative to the group |
| 238 |
\fI%\fR \fBlsort\fR \-stride 3 \-index {0 1} \e |
| 239 |
{{Bob Smith} 25 Audi {Jane Doe} 40 Ford} |
| 240 |
{{Jane Doe} 40 Ford {Bob Smith} 25 Audi} |
| 241 |
.CE |
| 242 |
.VE 8.6 |
| 243 |
.PP |
| 244 |
Stripping duplicate values using sorting: |
| 245 |
.PP |
| 246 |
.CS |
| 247 |
\fI%\fR \fBlsort\fR -unique {a b c a b c a b c} |
| 248 |
a b c |
| 249 |
.CE |
| 250 |
.PP |
| 251 |
More complex sorting using a comparison function: |
| 252 |
.PP |
| 253 |
.CS |
| 254 |
\fI%\fR proc compare {a b} { |
| 255 |
set a0 [lindex $a 0] |
| 256 |
set b0 [lindex $b 0] |
| 257 |
if {$a0 < $b0} { |
| 258 |
return -1 |
| 259 |
} elseif {$a0 > $b0} { |
| 260 |
return 1 |
| 261 |
} |
| 262 |
return [string compare [lindex $a 1] [lindex $b 1]] |
| 263 |
} |
| 264 |
\fI%\fR \fBlsort\fR -command compare \e |
| 265 |
{{3 apple} {0x2 carrot} {1 dingo} {2 banana}} |
| 266 |
{1 dingo} {2 banana} {0x2 carrot} {3 apple} |
| 267 |
.CE |
| 268 |
.SH "SEE ALSO" |
| 269 |
list(n), lappend(n), lindex(n), linsert(n), llength(n), lsearch(n), |
| 270 |
lset(n), lrange(n), lreplace(n) |
| 271 |
.SH KEYWORDS |
| 272 |
element, list, order, sort |
| 273 |
'\" Local Variables: |
| 274 |
'\" mode: nroff |
| 275 |
'\" End: |