| |||||||||||
| |||||||||||
Stable sorting - Tcl, Perl and others
Have you come across a STABLE sort? A Stable sort is one in which all the incoming elements which evaluate to an equal value when tested for sorting purposes remain in the same order in the output as they were in the input. Perhaps I had better give you an example.
I have a log file and there's a date in every line, but the lines are NOT in date order. And I want to sort my lines so that every line for each day is grouped together, by ascending date order. However, within any particular day I want my output records to remain in the same order that they were in in the input file. Input data: 20070903 John Smith etc 20070901 Bill Jones 20070902 Arthur Clark 20070903 George Hill 20070902 Arthur Dent 20070903 Petal Honour-Flower Stable sort output required: 20070901 Bill Jones 20070902 Arthur Clark 20070902 Arthur Dent 20070903 John Smith etc 20070903 George Hill 20070903 Petal Honour-Flower As compared to output from a standard (default) sort: 20070901 Bill Jones 20070902 Arthur Clark 20070902 Arthur Dent 20070903 George Hill 20070903 John Smith etc 20070903 Petal Honour-Flower Sorting in Tcl Here is the sample code to demonstrate those various sorts in Tcl: proc first {this that} {The lsort command sorts a list, returning a new list, and lsort -command does the same thing but allows the user to specify a proc which returns a negative, zero or positive value to indicate which of the records comes first (negative = no swap, positive = swap, zero = don't care). You'll note that it appears that Tcl provides a naturally stable sort in these circumstances, with and zero returns causing the output records to remain in their original order. I have seen no proof of this - the comment is empirical, but I have swapped by data lines around, and added and taken a few away, and it appears to hold. Sorting in Perl Here is the sample code to demonstrate those various sorts in Perl: The principles are very similar to those of Tcl, but the langauge is very different. Again, this appears to be a stable sort automatically. (written 2007-09-06 17:16:10) Associated topics are indexed under P211 - Perl - HashesT206 - Tcl/Tk - Lists T214 - Tcl/Tk - Other Facilities in Tcl
Some other Articles
Handling Binary data in Tcl (with a note on C)A series of tyre damages Ignore case in Regular Expression Expanding a grid - Tcl/Tk Stable sorting - Tcl, Perl and others Kasteel Elsloo - Michelin rated hotel. Melksham Hotel - Five Star Kitchen! MySQL joins revisited While waiting for Melksham Post Office Subway Restaurant in Melksham, Wiltshire 1638 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33 at 50 posts per pageThis is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price. Link to Ezine home page (for reading). Link to Blogging home page (to add comments). |
| ||||||||||
PH: 01144 1225 708225 • FAX: 01144 1225 707126 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho | |||||||||||