161 lines
		
	
	
		
			7.1 KiB
		
	
	
	
		
			HTML
		
	
	
	
	
	
			
		
		
	
	
			161 lines
		
	
	
		
			7.1 KiB
		
	
	
	
		
			HTML
		
	
	
	
	
	
| <HTML><HEAD><TITLE>Tcl Built-In Commands - lsort manual page</TITLE></HEAD><BODY>
 | |
| <DL>
 | |
| <DD><A HREF="lsort.htm#M2" NAME="L688">NAME</A>
 | |
| <DL><DD>lsort - Sort the elements of a list</DL>
 | |
| <DD><A HREF="lsort.htm#M3" NAME="L689">SYNOPSIS</A>
 | |
| <DL>
 | |
| <DD><B>lsort </B>?<I>options</I>? <I>list</I>
 | |
| </DL>
 | |
| <DD><A HREF="lsort.htm#M4" NAME="L690">DESCRIPTION</A>
 | |
| <DL>
 | |
| <DD><A HREF="lsort.htm#M5" NAME="L691"><B>-ascii</B></A>
 | |
| <DD><A HREF="lsort.htm#M6" NAME="L692"><B>-dictionary</B></A>
 | |
| <DD><A HREF="lsort.htm#M7" NAME="L693"><B>-integer</B></A>
 | |
| <DD><A HREF="lsort.htm#M8" NAME="L694"><B>-real</B></A>
 | |
| <DD><A HREF="lsort.htm#M9" NAME="L695"><B>-command </B><I>command</I></A>
 | |
| <DD><A HREF="lsort.htm#M10" NAME="L696"><B>-increasing</B></A>
 | |
| <DD><A HREF="lsort.htm#M11" NAME="L697"><B>-decreasing</B></A>
 | |
| <DD><A HREF="lsort.htm#M12" NAME="L698"><B>-index </B><I>index</I></A>
 | |
| <DD><A HREF="lsort.htm#M13" NAME="L699"><B>-unique</B></A>
 | |
| </DL>
 | |
| <DD><A HREF="lsort.htm#M14" NAME="L700">NOTES</A>
 | |
| <DD><A HREF="lsort.htm#M15" NAME="L701">EXAMPLES</A>
 | |
| <DD><A HREF="lsort.htm#M16" NAME="L702">SEE ALSO</A>
 | |
| <DD><A HREF="lsort.htm#M17" NAME="L703">KEYWORDS</A>
 | |
| </DL><HR>
 | |
| <H3><A NAME="M2">NAME</A></H3>
 | |
| lsort - Sort the elements of a list
 | |
| <H3><A NAME="M3">SYNOPSIS</A></H3>
 | |
| <B>lsort </B>?<I>options</I>? <I>list</I><BR>
 | |
| <H3><A NAME="M4">DESCRIPTION</A></H3>
 | |
| This command sorts the elements of <I>list</I>, returning a new
 | |
| list in sorted order.  The implementation of the <B>lsort</B> command
 | |
| uses the merge-sort algorithm which is a stable sort that has O(n log
 | |
| n) performance characteristics.
 | |
| <P>
 | |
| By default ASCII sorting is used with the result returned in
 | |
| increasing order.  However, any of the following options may be
 | |
| specified before <I>list</I> to control the sorting process (unique
 | |
| abbreviations are accepted):
 | |
| <P>
 | |
| <DL>
 | |
| <P><DT><A NAME="M5"><B>-ascii</B></A><DD>
 | |
| Use string comparison with ASCII collation order.  This is the default.
 | |
| <P><DT><A NAME="M6"><B>-dictionary</B></A><DD>
 | |
| Use dictionary-style comparison.  This is the same as <B>-ascii</B>
 | |
| except (a) case is ignored except as a tie-breaker and (b) if two
 | |
| strings contain embedded numbers, the numbers compare as integers,
 | |
| not characters.  For example, in <B>-dictionary</B> mode, <B>bigBoy</B>
 | |
| sorts between <B>bigbang</B> and <B>bigboy</B>, and <B>x10y</B>
 | |
| sorts between <B>x9y</B> and <B>x11y</B>.
 | |
| <P><DT><A NAME="M7"><B>-integer</B></A><DD>
 | |
| Convert list elements to integers and use integer comparison.
 | |
| <P><DT><A NAME="M8"><B>-real</B></A><DD>
 | |
| Convert list elements to floating-point values and use floating comparison.
 | |
| <P><DT><A NAME="M9"><B>-command </B><I>command</I></A><DD>
 | |
| Use <I>command</I> as a comparison command.
 | |
| To compare two elements, evaluate a Tcl script consisting of
 | |
| <I>command</I> with the two elements appended as additional
 | |
| arguments.  The script should return an integer less than,
 | |
| equal to, or greater than zero if the first element is to
 | |
| be considered less than, equal to, or greater than the second,
 | |
| respectively.
 | |
| <P><DT><A NAME="M10"><B>-increasing</B></A><DD>
 | |
| Sort the list in increasing order (``smallest'' items first).
 | |
| This is the default.
 | |
| <P><DT><A NAME="M11"><B>-decreasing</B></A><DD>
 | |
| Sort the list in decreasing order (``largest'' items first).
 | |
| <P><DT><A NAME="M12"><B>-index </B><I>index</I></A><DD>
 | |
| If this option is specified, each of the elements of <I>list</I> must
 | |
| itself be a proper Tcl sublist.  Instead of sorting based on whole
 | |
| sublists, <B>lsort</B> will extract the <I>index</I>'th element from
 | |
| each sublist and sort based on the given element.  The keyword
 | |
| <B>end</B> is allowed for the <I>index</I> to sort on the last sublist
 | |
| element,
 | |
| and <B>end-</B><I>index</I> sorts on a sublist element offset from
 | |
| the end.
 | |
| For example,
 | |
| <PRE>lsort -integer -index 1 {{First 24} {Second 18} {Third 30}}</PRE>
 | |
| returns <B>{Second 18} {First 24} {Third 30}</B>, and
 | |
| <PRE>lsort -index end-1 {{a 1 e i} {b 2 3 f g} {c 4 5 6 d h}}</PRE>
 | |
| returns <B>{c 4 5 6 d h} {a 1 e i} {b 2 3 f g}</B>.
 | |
| This option is much more efficient than using <B>-command</B>
 | |
| to achieve the same effect.
 | |
| <P><DT><A NAME="M13"><B>-unique</B></A><DD>
 | |
| If this option is specified, then only the last set of duplicate
 | |
| elements found in the list will be retained.  Note that duplicates are
 | |
| determined relative to the comparison used in the sort.  Thus if 
 | |
| <I>-index 0</I> is used, <B>{1 a}</B> and <B>{1 b}</B> would be
 | |
| considered duplicates and only the second element, <B>{1 b}</B>, would
 | |
| be retained.
 | |
| 
 | |
| <P></DL>
 | |
| <H3><A NAME="M14">NOTES</A></H3>
 | |
| The options to <B>lsort</B> only control what sort of comparison is
 | |
| used, and do not necessarily constrain what the values themselves
 | |
| actually are.  This distinction is only noticeable when the list to be
 | |
| sorted has fewer than two elements.
 | |
| <P>
 | |
| The <B>lsort</B> command is reentrant, meaning it is safe to use as
 | |
| part of the implementation of a command used in the <B>-command</B>
 | |
| option.
 | |
| 
 | |
| <H3><A NAME="M15">EXAMPLES</A></H3>
 | |
| Sorting a list using ASCII sorting:
 | |
| <PRE>% lsort {a10 B2 b1 a1 a2}
 | |
| B2 a1 a10 a2 b1</PRE>
 | |
| <P>
 | |
| Sorting a list using Dictionary sorting:
 | |
| <PRE>% lsort -dictionary {a10 B2 b1 a1 a2}
 | |
| a1 a2 a10 b1 B2</PRE>
 | |
| <P>
 | |
| Sorting lists of integers:
 | |
| <PRE>% lsort -integer {5 3 1 2 11 4}
 | |
| 1 2 3 4 5 11
 | |
| % lsort -integer {1 2 0x5 7 0 4 -1}
 | |
| -1 0 1 2 4 0x5 7</PRE>
 | |
| <P>
 | |
| Sorting lists of floating-point numbers:
 | |
| <PRE>% lsort -real {5 3 1 2 11 4}
 | |
| 1 2 3 4 5 11
 | |
| % lsort -real {.5 0.07e1 0.4 6e-1}
 | |
| 0.4 .5 6e-1 0.07e1</PRE>
 | |
| <P>
 | |
| Sorting using indices:
 | |
| <PRE>% # Note the space character before the c
 | |
| % lsort {{a 5} { c 3} {b 4} {e 1} {d 2}}
 | |
| { c 3} {a 5} {b 4} {d 2} {e 1}
 | |
| % lsort -index 0 {{a 5} { c 3} {b 4} {e 1} {d 2}}
 | |
| {a 5} {b 4} { c 3} {d 2} {e 1}
 | |
| % lsort -index 1 {{a 5} { c 3} {b 4} {e 1} {d 2}}
 | |
| {e 1} {d 2} { c 3} {b 4} {a 5}</PRE>
 | |
| <P>
 | |
| Stripping duplicate values using sorting:
 | |
| <PRE>% lsort -unique {a b c a b c a b c}
 | |
| a b c</PRE>
 | |
| <P>
 | |
| More complex sorting using a comparison function:
 | |
| <PRE>% proc compare {a b} {
 | |
|     set a0 [lindex $a 0]
 | |
|     set b0 [lindex $b 0]
 | |
|     if {$a0 < $b0} {
 | |
|         return -1
 | |
|     } elseif {$a0 > $b0} {
 | |
|         return 1
 | |
|     }
 | |
|     return [string compare [lindex $a 1] [lindex $b 1]]
 | |
| }
 | |
| % lsort -command compare \
 | |
|         {{3 apple} {0x2 carrot} {1 dingo} {2 banana}}
 | |
| {1 dingo} {2 banana} {0x2 carrot} {3 apple}</PRE>
 | |
| <H3><A NAME="M16">SEE ALSO</A></H3>
 | |
| <B><A HREF="../TkCmd/lappend.htm">lappend</A></B>, <B><A HREF="../TkCmd/lindex.htm">lindex</A></B>, <B><A HREF="../TkCmd/linsert.htm">linsert</A></B>, <B><A HREF="../TkCmd/list.htm">list</A></B>, <B><A HREF="../TkCmd/llength.htm">llength</A></B>, <B><A HREF="../TkCmd/lrange.htm">lrange</A></B>, <B><A HREF="../TkCmd/lreplace.htm">lreplace</A></B>, <B><A HREF="../TkCmd/lsearch.htm">lsearch</A></B>
 | |
| <H3><A NAME="M17">KEYWORDS</A></H3>
 | |
| <A href="../Keywords/E.htm#element">element</A>, <A href="../Keywords/L.htm#list">list</A>, <A href="../Keywords/O.htm#order">order</A>, <A href="../Keywords/S.htm#sort">sort</A>
 | |
| <HR><PRE>
 | |
| <A HREF="../copyright.htm">Copyright</A> © 1993 The Regents of the University of California.
 | |
| <A HREF="../copyright.htm">Copyright</A> © 1994-1996 Sun Microsystems, Inc.
 | |
| <A HREF="../copyright.htm">Copyright</A> © 1999 Scriptics Corporation
 | |
| <A HREF="../copyright.htm">Copyright</A> © 1995-1997 Roger E. Critchlow Jr.</PRE>
 | |
| </BODY></HTML>
 | 
