Loading ...
Sorry, an error occurred while loading the content.

Re: [PBML] Multiple Dimension Array with multiple sort

Expand Messages
  • merlyn@stonehenge.com
    ... Tim Suppose I have a multi-di array. I can make it sort by any one Tim element. But what I need to sort by two? In my simple example Tim below, what
    Message 1 of 12 , Apr 7 10:58 AM
    • 0 Attachment
      >>>>> "Tim" == Tim Lewis <twlewis@...> writes:

      Tim> Suppose I have a multi-di array. I can make it sort by any one
      Tim> element. But what I need to sort by two? In my simple example
      Tim> below, what if I wanted to sort by the animal name, and then by
      Tim> number of legs? I am grateful for any help or tips on this.

      Since that can be interpreted two ways, I'm going to pretend you
      actually said "with the major sort by animal name, using the number of
      legs to break ties as a secondary sort".

      Tim> my @critters; #multi-di with animal name and number of legs
      Tim> $critters[0][0] = "dog";
      Tim> $critters[0][1] = "4";
      Tim> $critters[1][0] = "cat";
      Tim> $critters[1][1] = "4";
      Tim> $critters[2][0] = "monkey";
      Tim> $critters[2][1] = "2";

      my @sorted = sort {
      $a->[0] cmp $b->[0]
      or $a->[1] <=> $b->[1];
      } @critters;

      This is covered in the Alpaca book.

      And now, for the traditional "don't post here" comment:

      **************************************************
      *
      * THIS IS NOT THE OFFICIAL perl BEGINNERS GROUP.
      *
      * The official one is at beginners@..., with instructions on how to
      * join at http://lists.perl.org/list/beginners.html.
      *
      * You'll get far better response there, not to say you won't get *any* here.
      *
      **************************************************


      --
      Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
      <merlyn@...> <URL:http://www.stonehenge.com/merlyn/>
      Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
      See http://methodsandmessages.posterous.com/ for Smalltalk discussion
    • Tim Lewis
      Thanks Randall. I was close. I was using cmp instead of for the number portion. Thanks again. I did not know about the other listserver, and found this
      Message 2 of 12 , Apr 7 11:10 AM
      • 0 Attachment
        Thanks Randall. I was close. I was using "cmp" instead of <=> for the number portion.

        Thanks again.
        I did not know about the other listserver, and found this one on Yahoo. I will check out the other one.


        Tim

        ---- merlyn@... wrote:
        > >>>>> "Tim" == Tim Lewis <twlewis@...> writes:
        >
        > Tim> Suppose I have a multi-di array. I can make it sort by any one
        > Tim> element. But what I need to sort by two? In my simple example
        > Tim> below, what if I wanted to sort by the animal name, and then by
        > Tim> number of legs? I am grateful for any help or tips on this.
        >
        > Since that can be interpreted two ways, I'm going to pretend you
        > actually said "with the major sort by animal name, using the number of
        > legs to break ties as a secondary sort".
        >
        > Tim> my @critters; #multi-di with animal name and number of legs
        > Tim> $critters[0][0] = "dog";
        > Tim> $critters[0][1] = "4";
        > Tim> $critters[1][0] = "cat";
        > Tim> $critters[1][1] = "4";
        > Tim> $critters[2][0] = "monkey";
        > Tim> $critters[2][1] = "2";
        >
        > my @sorted = sort {
        > $a->[0] cmp $b->[0]
        > or $a->[1] <=> $b->[1];
        > } @critters;
        >
        > This is covered in the Alpaca book.
        >
        > And now, for the traditional "don't post here" comment:
        >
        > **************************************************
        > *
        > * THIS IS NOT THE OFFICIAL perl BEGINNERS GROUP.
        > *
        > * The official one is at beginners@..., with instructions on how to
        > * join at http://lists.perl.org/list/beginners.html.
        > *
        > * You'll get far better response there, not to say you won't get *any* here.
        > *
        > **************************************************
        >
        >
        > --
        > Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
        > <merlyn@...> <URL:http://www.stonehenge.com/merlyn/>
        > Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
        > See http://methodsandmessages.posterous.com/ for Smalltalk discussion
      Your message has been successfully submitted and would be delivered to recipients shortly.