From gunza at missouri.edu Thu Nov 16 19:03:43 2006 From: gunza at missouri.edu (Gunz, Alexander J.) Date: Thu, 16 Nov 2006 21:03:43 -0600 Subject: [Columbia-pm] sorting In-Reply-To: Message-ID: <62C002D4040DAF4187F1B9B647A8F9E6011AEECF@UM-XMAIL03.um.umsystem.edu> Perl sorts numbers like this: 1 10 135,152 2 20,000,000 21 3 How do you make it sort them like this: 1 2 3 10 21 135,152 20,000,000 ??? The only way I can do it now is to work out how many decimals I'm likely to go to, and insert leading zeros ("001, 002", etc). Problematically, Perl sometimes interprets this as octal numbers and sorts them in even crazier ways. Can someone make my life easier? Thanks Alex From furtiveone at gmail.com Sun Nov 19 14:46:56 2006 From: furtiveone at gmail.com (Doug Leffert) Date: Sun, 19 Nov 2006 16:46:56 -0600 Subject: [Columbia-pm] sorting In-Reply-To: <62C002D4040DAF4187F1B9B647A8F9E6011AEECF@UM-XMAIL03.um.umsystem.edu> References: <62C002D4040DAF4187F1B9B647A8F9E6011AEECF@UM-XMAIL03.um.umsystem.edu> Message-ID: <9c56fc500611191446h7f9c572awcc24d65d99cd6b1c@mail.gmail.com> On 11/16/06, Gunz, Alexander J. wrote: ... > How do you make it sort them like this: > > 1 > 2 > 3 > 10 > 21 > 135,152 > 20,000,000 ... > Can someone make my life easier? >From 'perldoc -f sort': "If SUBNAME or BLOCK is omitted, "sort"s in standard string comparison order." So, use 'sort {$a <=> $b} @YOUR_ARRAY; See the following debugger session for an example using your data (DB<5> is where your expected sort occurs). $ perl -de 42 Loading DB routines from perl5db.pl version 1.28 Editor support available. Enter h or `h h' for help, or `man perldebug' for more help. main::(-e:1): 42 DB<1> @n = (1,10,135_152,2,20_000_000,21,3); DB<2> x @n 0 1 1 10 2 135152 3 2 4 20000000 5 21 6 3 DB<3> @sorted = sort @n; DB<4> x @sorted; 0 1 1 10 2 135152 3 2 4 20000000 5 21 6 3 DB<5> @x = sort {$a <=> $b} @n; DB<6> x @x 0 1 1 2 2 3 3 10 4 21 5 135152 6 20000000 Best, /doug