1lsort(n) Tcl Built-In Commands lsort(n)
2
3
4
5______________________________________________________________________________
6
8 lsort - Sort the elements of a list
9
11 lsort ?options? list
12_________________________________________________________________
13
14
16 This command sorts the elements of list, returning a new list in sorted
17 order. The implementation of the lsort command uses the merge-sort
18 algorithm which is a stable sort that has O(n log n) performance char‐
19 acteristics.
20
21 By default ASCII sorting is used with the result returned in increasing
22 order. However, any of the following options may be specified before
23 list to control the sorting process (unique abbreviations are
24 accepted):
25
26 -ascii Use string comparison with Unicode code-point col‐
27 lation order (the name is for backward-compatibil‐
28 ity reasons.) This is the default.
29
30 -dictionary Use dictionary-style comparison. This is the same
31 as -ascii except (a) case is ignored except as a
32 tie-breaker and (b) if two strings contain embedded
33 numbers, the numbers compare as integers, not char‐
34 acters. For example, in -dictionary mode, bigBoy
35 sorts between bigbang and bigboy, and x10y sorts
36 between x9y and x11y.
37
38 -integer Convert list elements to integers and use integer
39 comparison.
40
41 -real Convert list elements to floating-point values and
42 use floating comparison.
43
44 -command command Use command as a comparison command. To compare
45 two elements, evaluate a Tcl script consisting of
46 command with the two elements appended as addi‐
47 tional arguments. The script should return an
48 integer less than, equal to, or greater than zero
49 if the first element is to be considered less than,
50 equal to, or greater than the second, respectively.
51
52 -increasing Sort the list in increasing order (``smallest''
53 items first). This is the default.
54
55 -decreasing Sort the list in decreasing order (``largest''
56 items first).
57
58 -index index If this option is specified, each of the elements
59 of list must itself be a proper Tcl sublist.
60 Instead of sorting based on whole sublists, lsort
61 will extract the index'th element from each sublist
62 and sort based on the given element. The keyword
63 end is allowed for the index to sort on the last
64 sublist element, and end-index sorts on a sublist │
65 element offset from the end. For example,
66 lsort -integer -index 1 {{First 24} {Second 18} {Third 30}}
67 returns {Second 18} {First 24} {Third 30}, and │
68 lsort -index end-1 {{a 1 e i} {b 2 3 f g} {c 4 5 6 d h}}│
69 returns {c 4 5 6 d h} {a 1 e i} {b 2 3 f g}. This
70 option is much more efficient than using -command
71 to achieve the same effect.
72
73 -unique If this option is specified, then only the last set
74 of duplicate elements found in the list will be
75 retained. Note that duplicates are determined rel‐
76 ative to the comparison used in the sort. Thus if
77 -index 0 is used, {1 a} and {1 b} would be consid‐
78 ered duplicates and only the second element, {1 b},
79 would be retained.
80
82 The options to lsort only control what sort of comparison is used, and
83 do not necessarily constrain what the values themselves actually are.
84 This distinction is only noticeable when the list to be sorted has
85 fewer than two elements.
86
87 The lsort command is reentrant, meaning it is safe to use as part of
88 the implementation of a command used in the -command option.
89
91 Sorting a list using ASCII sorting:
92 % lsort {a10 B2 b1 a1 a2}
93 B2 a1 a10 a2 b1
94
95 Sorting a list using Dictionary sorting:
96 % lsort -dictionary {a10 B2 b1 a1 a2}
97 a1 a2 a10 b1 B2
98
99 Sorting lists of integers:
100 % lsort -integer {5 3 1 2 11 4}
101 1 2 3 4 5 11
102 % lsort -integer {1 2 0x5 7 0 4 -1}
103 -1 0 1 2 4 0x5 7
104
105 Sorting lists of floating-point numbers:
106 % lsort -real {5 3 1 2 11 4}
107 1 2 3 4 5 11
108 % lsort -real {.5 0.07e1 0.4 6e-1}
109 0.4 .5 6e-1 0.07e1
110
111 Sorting using indices:
112 % # Note the space character before the c
113 % lsort {{a 5} { c 3} {b 4} {e 1} {d 2}}
114 { c 3} {a 5} {b 4} {d 2} {e 1}
115 % lsort -index 0 {{a 5} { c 3} {b 4} {e 1} {d 2}}
116 {a 5} {b 4} { c 3} {d 2} {e 1}
117 % lsort -index 1 {{a 5} { c 3} {b 4} {e 1} {d 2}}
118 {e 1} {d 2} { c 3} {b 4} {a 5}
119
120 Stripping duplicate values using sorting:
121 % lsort -unique {a b c a b c a b c}
122 a b c
123
124 More complex sorting using a comparison function:
125 % proc compare {a b} {
126 set a0 [lindex $a 0]
127 set b0 [lindex $b 0]
128 if {$a0 < $b0} {
129 return -1
130 } elseif {$a0 > $b0} {
131 return 1
132 }
133 return [string compare [lindex $a 1] [lindex $b 1]]
134 }
135 % lsort -command compare \
136 {{3 apple} {0x2 carrot} {1 dingo} {2 banana}}
137 {1 dingo} {2 banana} {0x2 carrot} {3 apple}
138
139
141 list(n), lappend(n), lindex(n), linsert(n), llength(n), lsearch(n), │
142 lset(n), lrange(n), lreplace(n)
143
144
146 element, list, order, sort
147
148
149
150Tcl 8.3 lsort(n)