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

Discussion in 'Perl Misc' started by Tomasz Chmielewski, Mar 4, 2008.

  1. 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
     
    Tomasz Chmielewski, Mar 4, 2008
    #1
    1. Advertising

  2. Tomasz Chmielewski <> 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
     
    Peter Makholm, Mar 4, 2008
    #2
    1. Advertising

  3. Peter Makholm <> writes:

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


    Yes, this code contains some obvious errors.

    //Makholm
     
    Peter Makholm, Mar 4, 2008
    #3
  4. Peter Makholm schrieb:
    > Tomasz Chmielewski <> 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
     
    Tomasz Chmielewski, Mar 4, 2008
    #4
  5. Abigail schrieb:
    > _
    > Tomasz Chmielewski () wrote on VCCXCIX September
    > MCMXCIII in <URL:news:fqjgls$cnj$>:
    > @@ 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
     
    Tomasz Chmielewski, Mar 4, 2008
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tim Daneliuk
    Replies:
    0
    Views:
    271
    Tim Daneliuk
    Sep 25, 2006
  2. Replies:
    0
    Views:
    370
  3. Replies:
    2
    Views:
    1,441
    James Kanze
    Jul 6, 2010
  4. Simon Strandgaard
    Replies:
    0
    Views:
    105
    Simon Strandgaard
    Feb 23, 2004
  5. Ruby Quiz
    Replies:
    5
    Views:
    125
    Michael Fellinger
    Jan 21, 2007
Loading...

Share This Page