For Programmers: Free Programming Magazines  


Home > Archive > PERL on RISC OS > June 2005 > Sorting









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Sorting
Colin Foster

2005-06-08, 9:04 pm


Hi All,

I'm sorting a list in numerical order using
@new = sort {$b <=> $a} @old;

Is it possible to restrict the sort so that it only sorts on the first
4 digits.

Regards,
Colin.
Derek.Moody

2005-06-08, 9:04 pm

On Thu 01 Jul, Colin Foster wrote:

> I'm sorting a list in numerical order using
> @new = sort {$b <=> $a} @old;
>
> Is it possible to restrict the sort so that it only sorts on the first
> 4 digits.


Presuming your data are well regulated try:

@new = sort {substr($b,0,4) <=> substr($a,0,4)} @old;

(Following your $b $a count-down order)

Cheerio,

--[color=darkred]

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com