Date sent: Wed, 14 Oct 2009 11:03:13 -0400 From: Shawn H Corey <shawnhco...@gmail.com> To: Rick Triplett <r...@reason.net> Copies to: Perl Beginners <beginners@perl.org> Subject: Re: Sorting mixed alphanumerics
> Rick Triplett wrote: > > I need to sort the keys in a hash. The keys are the question number and > > the values are the student's answer. A numeric sort with <=> won't work > > since retaking a missed question (say, 2) produces the new key, 2h with > > its new answer. A representative hash might look like this > > You need a Schwartzian transform. It works by extract the sort fields > from the data and placing them, and the original data, in an array of > arrays (AoA). It sorts the AoA and extracts the original data from > them. This sorted list now can be used. ST is an overkill if the extraction is simple. Especially if the number of items is fairly small. Actually if the extraction is really simple and the extracted key is not so small, than ST may perform worse than an ordinary sort doing the extraction within the comparison block. The additional array creation and destruction and increased memory load will more than ofset the lower number of extractions. Jenda ===== je...@krynicky.cz === http://Jenda.Krynicky.cz ===== When it comes to wine, women and song, wizards are allowed to get drunk and croon as much as they like. -- Terry Pratchett in Sourcery -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/