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

  • Thread starter Tomasz Chmielewski
  • Start date
T

Tomasz Chmielewski

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?
 
P

Peter Makholm

Tomasz Chmielewski said:
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
 
P

Peter Makholm

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

Yes, this code contains some obvious errors.

//Makholm
 
T

Tomasz Chmielewski

Peter said:
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];
}
 
T

Tomasz Chmielewski

Abigail said:
_
Tomasz Chmielewski ([email protected]) wrote on VCCXCIX September
MCMXCIII in <URL:mad:@ 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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,733
Messages
2,569,439
Members
44,829
Latest member
PIXThurman

Latest Threads

Top