Click here to get back home

sorting index-15, index-9, index-110 "the human way"?

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
sorting index-15, index-9, index-110 "the human way"? Tomasz Chmielewski 03-04-2008
Get Chitika Premium
Posted by Tomasz Chmielewski on March 4, 2008, 7:52 am
Please log in for more thread options
Let's say I have an array consisting of:

some-name-10
some-name-9
some-name-102
some-name-89

I would like to sort it the "human way", with the result:

some-name-9
some-name-10
some-name-89
some-name-102


What would be the suggested way to do it?

I would use 'split', but "some-name" is not static, and can be
"some-other-name" or just "name" a task later.

For the same reason, using 'substr' won't work.

Unless I write a subroutine to it.


What do you guys use to sort such things?



--
Tomasz Chmielewski
http://wpkg.org


Posted by Peter Makholm on March 4, 2008, 7:59 am
Please log in for more thread options

> I would use 'split', but "some-name" is not static, and can be
> "some-other-name" or just "name" a task later.
>
> For the same reason, using 'substr' won't work.
>
> Unless I write a subroutine to it.

Yes, you have to tell us (and perl) how to extract the number you want
to sort the lines after.

And the you sorting is trivial:

@sorted = sort { extract($a) <=> extract($b) } @unsorted

or by using some of the standard optimizations

@sorted = map { $_->[0] }
sort { $a->[0] <=> $b->[0] }
map { [ extract($_), $_ ] } @unsorted

or one of the orthers Sort::Maker can do for you.

//Makholm

Posted by Peter Makholm on March 4, 2008, 7:59 am
Please log in for more thread options

> @sorted = map { $_->[0] }
> sort { $a->[0] <=> $b->[0] }
> map { [ extract($_), $_ ] } @unsorted

Yes, this code contains some obvious errors.

//Makholm

Posted by Tomasz Chmielewski on March 4, 2008, 8:14 am
Please log in for more thread options
Peter Makholm schrieb:
>
>> I would use 'split', but "some-name" is not static, and can be
>> "some-other-name" or just "name" a task later.
>>
>> For the same reason, using 'substr' won't work.
>>
>> Unless I write a subroutine to it.
>
> Yes, you have to tell us (and perl) how to extract the number you want
> to sort the lines after.
>
> And the you sorting is trivial:
>
> @sorted = sort { extract($a) <=> extract($b) } @unsorted

I guess I'll just use something like, it does not have to be
super-efficient:

sub extract() {
my $name = shift;
my @columns = split /-/, $name;
my $last_column = $columns[$#columns];
}



--
Tomasz Chmielewski
http://wpkg.org

Posted by bugbear on March 4, 2008, 8:29 am
Please log in for more thread options
Tomasz Chmielewski wrote:
> Peter Makholm schrieb:
>>
>>> I would use 'split', but "some-name" is not static, and can be
>>> "some-other-name" or just "name" a task later.
>>>
>>> For the same reason, using 'substr' won't work.
>>>
>>> Unless I write a subroutine to it.
>>
>> Yes, you have to tell us (and perl) how to extract the number you want
>> to sort the lines after.
>>
>> And the you sorting is trivial:
>>
>> @sorted = sort { extract($a) <=> extract($b) } @unsorted
>
> I guess I'll just use something like, it does not have to be
> super-efficient:
>
> sub extract() {
> my $name = shift;
> my @columns = split /-/, $name;
> my $last_column = $columns[$#columns];
> }

or specifically go for trailing digits?

sub extract {
my $name = shift;
$name =~ m/(\d+)$/;
return $1;
}

Similar ThreadsPosted
human nature of perl (new operators etc) November 11, 2004, 11:37 am
Sorting November 28, 2004, 10:43 pm
sorting July 7, 2005, 5:04 am
Sorting June 7, 2007, 4:44 am
Sorting an Array by Another January 21, 2005, 12:56 pm
sorting a structure May 5, 2005, 3:22 pm
Hash Sorting June 14, 2005, 2:49 pm
table sorting May 3, 2006, 5:58 pm
Sorting EBCDIC December 15, 2006, 11:48 am
Sorting Hash by Value and Key May 17, 2007, 9:57 am

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap