<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE TIP SYSTEM "http://www.tcl.tk/cgi-bin/tct/tip/tipxml.dtd">
<!-- Converted at Wed Jun 19 14:51:36 GMT 2013 -->
<!-- TIP AutoGenerator - written by Donal K. Fellows -->

<TIP number='326'>
<header><title>Add -stride Option to lsort</title><author address="mailto:kieran@dunelm.org.uk">Kieran Elby</author><status type='project' state='final' tclversion="8.6" vote='after'>$Revision: 1.5 $</status><history></history><created day='1' month='sep' year='2008' /><keyword>Tcl lsort sorting</keyword></header>
<abstract>This TIP adds a new option, <emph style="bold">-stride</emph>, to <emph style="bold">lsort</emph> to request that a list be treated as consisting of repeated groups of elements (as opposed to sublists), and that the the groups be sorted according to a chosen element within each group.</abstract>
<body><section title="Rationale">
<para>Flat name-value pair lists are common in Tcl - consider the output of <emph style="bold">array get</emph> and <emph style="bold">dict create</emph> or the input to <emph style="bold">foreach</emph>.</para>
<para>It is surprising then that there is no command to directly sort such a list by either the name or the value elements, while preserving the name-value mapping.</para>
<para>Doing so currently requires turning a name-value pair list into a list of sublists, using the <emph style="bold">lsort</emph> <emph style="italic">-index</emph> option to sort them, then flattening the list again, which is rather fiddly and inefficient.</para>
<para>While sorting name-value pair lists is no doubt the most common use case, lists containing groups of more than two elements are also reasonably common, and so providing support for groups of any size seems useful and no harder to implement.</para>
<para>The option format follows the Feature Request here: <url ref="https://sourceforge.net/support/tracker.php?aid=747083"/></para>
</section>
<section title="Proposed Change">
<para>A new option, <emph style="bold">-stride</emph>, taking one parameter, <emph style="italic">grpSize</emph> will be added to the <emph style="bold">lsort</emph> command.</para>
<para>If <emph style="bold">-stride</emph> is supplied, the list will be treated as consisting of groups of <emph style="italic">grpSize</emph> elements, and the groups will be sorted by either their first element or, if the <emph style="bold">-index</emph> option is used, by the element within each group given by the first index passed to <emph style="bold">-index</emph> (which is then ignored by <emph style="bold">-index</emph>).</para>
<para>Elements always remain in the same position within their group.</para>
<para>The list length must be a multiple of <emph style="italic">grpSize</emph>, which in turn must be at least 2.</para>
</section>
<section title="Examples">
<verbatim><vline encoding='base64'>IGxzb3J0IC1zdHJpZGUgMiB7Y2Fycm90IDEwIGFwcGxlIDUwIGJhbmFuYSAyNX0=</vline></verbatim>
<para>returns &quot;apple 50 banana 25 carrot 10&quot;</para>
<verbatim><vline encoding='base64'>IGxzb3J0IC1zdHJpZGUgMiAtaW5kZXggMSAtaW50ZWdlciB7Y2Fycm90IDEwIGFwcGxlIDUwIGJhbmFuYSAyNX0=</vline></verbatim>
<para>returns &quot;carrot 10 banana 25 apple 50&quot;</para>
<verbatim><vline encoding='base64'>IGxzb3J0IC1zdHJpZGUgMyAtaW5kZXggezAgMX0ge3tCb2IgU21pdGh9IDI1IEF1ZGkge0phbmUgRG9lfSA0MCBGb3JkfQ==</vline></verbatim>
<para>returns &quot;{Jane Doe} 40 Ford {Bob Smith} 25 Audi&quot; (since Smith, which is at index 1 of element 0 of the first group comes after Doe, which is at the same position in the next group of 3.)</para>
</section>
<section title="Reference Implementation">
<para>Available online at <url ref="https://sourceforge.net/support/tracker.php?aid=2082681"/></para>
</section>
<section title="Compatibility">
<para>Since this is a new and unambiguous option, and since the distinction between the options and the list to sort is clear, no compatibility problems are expected.</para>
</section>
<section title="Limitations">
<para>Only one element in each group can be used for comparison; it&apos;s not possible to compare groups element-by-element.</para>
</section>
<section title="Further Thoughts">
<para>There may be some merit in adding a similar option to <emph style="bold">lsearch</emph>.</para>
<para>The name-value pair case is common enough that an argument could be made for adding <emph style="bold">-pairname</emph> and <emph style="bold">-pairvalue</emph> options as synonyms for <emph style="bold">-stride 2 -index 0</emph> and <emph style="bold">-stride 2 -index 1</emph> respectively.</para>
</section>
<section title="Copyright">
<para>This document has been placed in the public domain.</para>
</section>
</body></TIP>
