Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/tcl8.5.2/doc/lsort.n @ 25

Last change on this file since 25 was 25, checked in by landauf, 16 years ago

added tcl to libs

File size: 6.1 KB
Line 
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 <kennykb@acm.org>.  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'\" RCS: @(#) $Id: lsort.n,v 1.29 2008/03/26 09:59:22 dkf Exp $
11'\"
12.so man.macros
13.TH lsort n 8.5 Tcl "Tcl Built-In Commands"
14.BS
15'\" Note:  do not modify the .SH NAME line immediately below!
16.SH NAME
17lsort \- Sort the elements of a list
18.SH SYNOPSIS
19\fBlsort \fR?\fIoptions\fR? \fIlist\fR
20.BE
21
22.SH DESCRIPTION
23.PP
24This command sorts the elements of \fIlist\fR, returning a new
25list in sorted order.  The implementation of the \fBlsort\fR command
26uses the merge\-sort algorithm which is a stable sort that has O(n log
27n) performance characteristics.
28.PP
29By default ASCII sorting is used with the result returned in
30increasing order.  However, any of the following options may be
31specified before \fIlist\fR to control the sorting process (unique
32abbreviations are accepted):
33.TP 20
34\fB\-ascii\fR
35Use string comparison with Unicode code-point collation order (the
36name is for backward-compatibility reasons.)  This is the default.
37.TP 20
38\fB\-dictionary\fR
39Use dictionary-style comparison.  This is the same as \fB\-ascii\fR
40except (a) case is ignored except as a tie-breaker and (b) if two
41strings contain embedded numbers, the numbers compare as integers,
42not characters.  For example, in \fB\-dictionary\fR mode, \fBbigBoy\fR
43sorts between \fBbigbang\fR and \fBbigboy\fR, and \fBx10y\fR
44sorts between \fBx9y\fR and \fBx11y\fR.
45.TP 20
46\fB\-integer\fR
47Convert list elements to integers and use integer comparison.
48.TP 20
49\fB\-real\fR
50Convert list elements to floating-point values and use floating comparison.
51.TP 20
52\fB\-command\0\fIcommand\fR
53Use \fIcommand\fR as a comparison command.
54To compare two elements, evaluate a Tcl script consisting of
55\fIcommand\fR with the two elements appended as additional
56arguments.  The script should return an integer less than,
57equal to, or greater than zero if the first element is to
58be considered less than, equal to, or greater than the second,
59respectively.
60.TP 20
61\fB\-increasing\fR
62Sort the list in increasing order
63.PQ smallest "items first" .
64This is the default.
65.TP 20
66\fB\-decreasing\fR
67Sort the list in decreasing order
68.PQ largest "items first" .
69.TP 20
70\fB\-indices\fR
71.VS "8.5 (TIP#217)"
72Return a list of indices into \fIlist\fR in sorted order instead of
73the values themselves.
74.VE "8.5 (TIP#217)"
75.TP 20
76\fB\-index\0\fIindexList\fR
77If this option is specified, each of the elements of \fIlist\fR must
78itself be a proper Tcl sublist.  Instead of sorting based on whole
79sublists, \fBlsort\fR will extract the \fIindexList\fR'th element from
80each sublist
81.VS 8.5
82(as if the overall element and the \fIindexList\fR were passed to
83\fBlindex\fR) and sort based on the given element. 
84.VE 8.5
85For example,
86.RS
87.CS
88lsort -integer -index 1 \e
89      {{First 24} {Second 18} {Third 30}}
90.CE
91returns \fB{Second 18} {First 24} {Third 30}\fR, and
92'\"
93'\" This example is from the test suite!
94'\"
95.CS
96lsort -index end-1 \e
97      {{a 1 e i} {b 2 3 f g} {c 4 5 6 d h}}
98.CE
99returns \fB{c 4 5 6 d h} {a 1 e i} {b 2 3 f g}\fR,
100.VS 8.5
101and
102.CS
103lsort -index {0 1} {
104   {{b i g} 12345}
105   {{d e m o} 34512}
106   {{c o d e} 54321}
107}
108.CE
109returns \fB{{d e m o} 34512} {{b i g} 12345} {{c o d e} 54321}\fR
110(because \fBe\fR sorts before \fBi\fR which sorts before \fBo\fR.)
111.VE 8.5
112This option is much more efficient than using \fB\-command\fR
113to achieve the same effect.
114.RE
115.VS 8.5
116.TP 20
117\fB\-nocase\fR
118Causes comparisons to be handled in a case-insensitive manner.  Has no
119effect if combined with the \fB\-dictionary\fR, \fB\-integer\fR, or
120\fB\-real\fR options.
121.VE 8.5
122.TP 20
123\fB\-unique\fR
124If this option is specified, then only the last set of duplicate
125elements found in the list will be retained.  Note that duplicates are
126determined relative to the comparison used in the sort.  Thus if
127\fI\-index 0\fR is used, \fB{1 a}\fR and \fB{1 b}\fR would be
128considered duplicates and only the second element, \fB{1 b}\fR, would
129be retained.
130.SH "NOTES"
131.PP
132The options to \fBlsort\fR only control what sort of comparison is
133used, and do not necessarily constrain what the values themselves
134actually are.  This distinction is only noticeable when the list to be
135sorted has fewer than two elements.
136.PP
137The \fBlsort\fR command is reentrant, meaning it is safe to use as
138part of the implementation of a command used in the \fB\-command\fR
139option.
140.SH "EXAMPLES"
141.PP
142Sorting a list using ASCII sorting:
143.CS
144% \fBlsort\fR {a10 B2 b1 a1 a2}
145B2 a1 a10 a2 b1
146.CE
147.PP
148Sorting a list using Dictionary sorting:
149.CS
150% \fBlsort\fR -dictionary {a10 B2 b1 a1 a2}
151a1 a2 a10 b1 B2
152.CE
153.PP
154Sorting lists of integers:
155.CS
156% \fBlsort\fR -integer {5 3 1 2 11 4}
1571 2 3 4 5 11
158% \fBlsort\fR -integer {1 2 0x5 7 0 4 -1}
159-1 0 1 2 4 0x5 7
160.CE
161.PP
162Sorting lists of floating-point numbers:
163.CS
164% \fBlsort\fR -real {5 3 1 2 11 4}
1651 2 3 4 5 11
166% \fBlsort\fR -real {.5 0.07e1 0.4 6e-1}
1670.4 .5 6e-1 0.07e1
168.CE
169.PP
170Sorting using indices:
171.CS
172% # Note the space character before the c
173% \fBlsort\fR {{a 5} { c 3} {b 4} {e 1} {d 2}}
174{ c 3} {a 5} {b 4} {d 2} {e 1}
175% \fBlsort\fR -index 0 {{a 5} { c 3} {b 4} {e 1} {d 2}}
176{a 5} {b 4} { c 3} {d 2} {e 1}
177% \fBlsort\fR -index 1 {{a 5} { c 3} {b 4} {e 1} {d 2}}
178{e 1} {d 2} { c 3} {b 4} {a 5}
179.CE
180.PP
181Stripping duplicate values using sorting:
182.CS
183% \fBlsort\fR -unique {a b c a b c a b c}
184a b c
185.CE
186.PP
187More complex sorting using a comparison function:
188.CS
189% proc compare {a b} {
190    set a0 [lindex $a 0]
191    set b0 [lindex $b 0]
192    if {$a0 < $b0} {
193        return -1
194    } elseif {$a0 > $b0} {
195        return 1
196    }
197    return [string compare [lindex $a 1] [lindex $b 1]]
198}
199% \fBlsort\fR -command compare \e
200        {{3 apple} {0x2 carrot} {1 dingo} {2 banana}}
201{1 dingo} {2 banana} {0x2 carrot} {3 apple}
202.CE
203
204.SH "SEE ALSO"
205list(n), lappend(n), lindex(n), linsert(n), llength(n), lsearch(n),
206lset(n), lrange(n), lreplace(n)
207
208.SH KEYWORDS
209element, list, order, sort
Note: See TracBrowser for help on using the repository browser.