Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > sorting index-15, index-9, index-110 "the human way"?

Reply
Thread Tools

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

 
 
Tomasz Chmielewski
Guest
Posts: n/a
 
      03-04-2008
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

 
Reply With Quote
 
 
 
 
Peter Makholm
Guest
Posts: n/a
 
      03-04-2008
Tomasz Chmielewski <(E-Mail Removed)> writes:

> 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
 
Reply With Quote
 
 
 
 
Peter Makholm
Guest
Posts: n/a
 
      03-04-2008
Peter Makholm <(E-Mail Removed)> writes:

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


Yes, this code contains some obvious errors.

//Makholm
 
Reply With Quote
 
Tomasz Chmielewski
Guest
Posts: n/a
 
      03-04-2008
Peter Makholm schrieb:
> Tomasz Chmielewski <(E-Mail Removed)> writes:
>
>> 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
 
Reply With Quote
 
Tomasz Chmielewski
Guest
Posts: n/a
 
      03-04-2008
Abigail schrieb:
> _
> Tomasz Chmielewski ((E-Mail Removed)) wrote on VCCXCIX September
> MCMXCIII in <URL:news:fqjgls$cnj$(E-Mail Removed)>:
> @@ 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?
>
>
> Well, that depends. Your specification, or rather lack of specification,
> allows multiple interpretations. If you have:
>
> sort-name-20
> sort-name-7
> name-15
> name-8
>
> do you want:
>
> name-8
> name-15
> sort-name-7
> sort-name-20
>
> or
>
> sort-name-7
> name-8
> name-15
> sort-name-20


The latter. And the "name" would always be the same (it would only
change in the next iteration; but for a given iteration, all names would
be the same).

So essentially, as I wrote:

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

with the result:

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


But I think "sub extract" discussed earlier is simple enough for this
case, and I used it.



--
Tomasz Chmielewski
http://wpkg.org
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Sorting list vs sorting vector boltar2003@boltar.world C++ 2 07-06-2010 09:40 AM
fired event Sorting which wasn't handled - sorting and SelectedIndexChanged Jason ASP .Net Web Controls 0 10-04-2006 02:19 PM
HUMAN SOFTWARE DEVELOPMENT NETWORK : you're welcome HUMAN SOFTWARE DEVELOPMENT NETWORK ASP .Net 3 07-05-2005 04:31 PM
Is there any human alive that can answer this question? Enrico Palazo ASP .Net 8 01-12-2004 12:03 PM
sorting by multiple criterias (sub-sorting) Tom Kirchner Perl Misc 3 10-11-2003 05:16 PM



Advertisments