Path: utzoo!utgpu!news-server.csri.toronto.edu!mailrus!wuarchive!zaphod.mps.ohio-state.edu!sdd.hp.com!ucsd!ucbvax!iwarp.intel.com!news From: merlyn@iwarp.intel.com (Randal Schwartz) Newsgroups: comp.lang.perl Subject: Re: sort Message-ID: <1990Aug29.063639.15479@iwarp.intel.com> Date: 29 Aug 90 06:36:39 GMT References: <1990Aug21.202005.26275@uvaarpa.Virginia.EDU> <1990Aug21.224327.20194@iwarp.intel.com> <1990Aug22.111018.3329@comp.vuw.ac.nz> Sender: news@iwarp.intel.com Reply-To: merlyn@iwarp.intel.com (Randal Schwartz) Organization: Stonehenge; netaccess via Intel, Beaverton, Oregon, USA Lines: 36 In-Reply-To: Mike.McManus@FtCollins.NCR.com (Mike McManus) In article , Mike.McManus@FtCollins (Mike McManus) writes: | Interestingly enuff, I had need for a similar sorting routine today, but with a | twist: I want to sort the indices of an associative array that are of the form | "A0, A1, A2, ..., A9, A10, ..." Of course an alphabetic sort returns "A0, A10, | A11, A19, ..., A1, ...", not what I want! | | Any simple solutions? Thanks! Well, two come to mind. The first one is pretty trivial source code: sub by_the_numbers_mostly { substr($a,1,999) > substr($b,1,999) ? 1 : -1; } However, I haven't tested this for speed (it's doing a lot of work in those substr's over and over and over again). What you might want to do is build a parallel array: @foo=('A10'..'A19','A0'..'A9'); # not in order, for demo grep(s/^.//,@fookey = @foo); sub byfookey { $fookey[$a] > $fookey[$b] ? 1 : -1; } @sortfoo = @foo[sort byfookey $[..$#foo]; print "@sortfoo"; For large arrays, I believe this would win. For small arrays, the first would probably win (I haven't tested that... if someone has a few more minutes than me, go ahead and please let us know). print "Just another Perl hacker," -- /=Randal L. Schwartz, Stonehenge Consulting Services (503)777-0095 ==========\ | on contract to Intel's iWarp project, Beaverton, Oregon, USA, Sol III | | merlyn@iwarp.intel.com ...!any-MX-mailer-like-uunet!iwarp.intel.com!merlyn | \=Cute Quote: "Welcome to Portland, Oregon, home of the California Raisins!"=/