1lsort(n)                     Tcl Built-In Commands                    lsort(n)
2
3
4
5______________________________________________________________________________
6

NAME

8       lsort - Sort the elements of a list
9

SYNOPSIS

11       lsort ?options? list
12_________________________________________________________________
13
14

DESCRIPTION

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”items
53                           first).  This is the default.
54
55       -decreasing         Sort the list in decreasing  order  (“largest”items
56                           first).
57
58       -indices            Return  a list of indices into list in sorted order │
59                           instead of the values themselves.
60
61       -index indexList    If this option is specified, each of  the  elements
62                           of  list  must  itself  be  a  proper  Tcl sublist.
63                           Instead of sorting based on whole  sublists,  lsort
64                           will  extract  the  indexList'th  element from each
65                           sublist  (as  if  the  overall  element   and   the │
66                           indexList  were passed to lindex) and sort based on │
67                           the given element.  For example,
68                                  lsort -integer -index 1 \
69                                        {{First 24} {Second 18} {Third 30}}
70                           returns {Second 18} {First 24} {Third 30}, and
71                                  lsort -index end-1 \
72                                        {{a 1 e i} {b 2 3 f g} {c 4 5 6 d h}}
73                           returns {c 4 5 6 d h} {a 1 e i} {b 2 3 f g}, and    │
74                                  lsort -index {0 1} {                         │
75                                     {{b i g} 12345}                           │
76                                     {{d e m o} 34512}                         │
77                                     {{c o d e} 54321}                         │
78                                  }                                            │
79                           returns {{d e m o} 34512} {{b i g} 12345} {{c  o  d 
80                           e}  54321}  (because  e  sorts before i which sorts │
81                           before o.)  This option is much more efficient than
82                           using -command to achieve the same effect.
83
84       -nocase                                                                 
85                           Causes comparisons to be handled in a case-insensi‐ │
86                           tive manner.  Has no effect if  combined  with  the │
87                           -dictionary, -integer, or -real options.
88
89       -unique             If this option is specified, then only the last set
90                           of duplicate elements found in  the  list  will  be
91                           retained.  Note that duplicates are determined rel‐
92                           ative to the comparison used in the sort.  Thus  if
93                           -index  0 is used, {1 a} and {1 b} would be consid‐
94                           ered duplicates and only the second element, {1 b},
95                           would be retained.
96

NOTES

98       The  options to lsort only control what sort of comparison is used, and
99       do not necessarily constrain what the values themselves  actually  are.
100       This  distinction  is  only  noticeable  when the list to be sorted has
101       fewer than two elements.
102
103       The lsort command is reentrant, meaning it is safe to use  as  part  of
104       the implementation of a command used in the -command option.
105

EXAMPLES

107       Sorting a list using ASCII sorting:
108              % lsort {a10 B2 b1 a1 a2}
109              B2 a1 a10 a2 b1
110
111       Sorting a list using Dictionary sorting:
112              % lsort -dictionary {a10 B2 b1 a1 a2}
113              a1 a2 a10 b1 B2
114
115       Sorting lists of integers:
116              % lsort -integer {5 3 1 2 11 4}
117              1 2 3 4 5 11
118              % lsort -integer {1 2 0x5 7 0 4 -1}
119              -1 0 1 2 4 0x5 7
120
121       Sorting lists of floating-point numbers:
122              % lsort -real {5 3 1 2 11 4}
123              1 2 3 4 5 11
124              % lsort -real {.5 0.07e1 0.4 6e-1}
125              0.4 .5 6e-1 0.07e1
126
127       Sorting using indices:
128              % # Note the space character before the c
129              % lsort {{a 5} { c 3} {b 4} {e 1} {d 2}}
130              { c 3} {a 5} {b 4} {d 2} {e 1}
131              % lsort -index 0 {{a 5} { c 3} {b 4} {e 1} {d 2}}
132              {a 5} {b 4} { c 3} {d 2} {e 1}
133              % lsort -index 1 {{a 5} { c 3} {b 4} {e 1} {d 2}}
134              {e 1} {d 2} { c 3} {b 4} {a 5}
135
136       Stripping duplicate values using sorting:
137              % lsort -unique {a b c a b c a b c}
138              a b c
139
140       More complex sorting using a comparison function:
141              % proc compare {a b} {
142                  set a0 [lindex $a 0]
143                  set b0 [lindex $b 0]
144                  if {$a0 < $b0} {
145                      return -1
146                  } elseif {$a0 > $b0} {
147                      return 1
148                  }
149                  return [string compare [lindex $a 1] [lindex $b 1]]
150              }
151              % lsort -command compare \
152                      {{3 apple} {0x2 carrot} {1 dingo} {2 banana}}
153              {1 dingo} {2 banana} {0x2 carrot} {3 apple}
154
155

SEE ALSO

157       list(n),  lappend(n),  lindex(n),  linsert(n),  llength(n), lsearch(n),
158       lset(n), lrange(n), lreplace(n)
159
160

KEYWORDS

162       element, list, order, sort
163
164
165
166Tcl                                   8.5                             lsort(n)
Impressum