in reply to Re: Re: Re: Re: Re: sort array by value in it
in thread sort array by value in it

Hi L~R. Im just curious about one part of your solution:

for ( sort { $data[$a]->[2] <=> $data[$b]->[2] } 0 .. $#data ) { print join "\t" , @{ $data[$_] }; print "\n"; }

Why did you code it this way I wonder? It seems odd that you are actually sorting a list of indexes and not the elements themselves. Theres no speed gain, in fact theres a loss in that each element requires two levels of dereferencing, and you have the added memory hit of constructing a list of indexes. Im really curious if there was any reason why you didnt just say

print join("\t",@$_),"\n" for sort { $a->[2] <=> $b->[2] } @data;

or possibly:

print join "\n",map { join "\t",@$_ } sort { $a->[2] <=>$b->[2] } @dat +a;

---
demerphq

    First they ignore you, then they laugh at you, then they fight you, then you win.
    -- Gandhi


Replies are listed 'Best First'.
Re: Re^6: sort array by value in it
by Limbic~Region (Chancellor) on May 04, 2004 at 20:33 UTC
    demerphq,
    Standing by itself, there is no reason. I changed my mind on the data structure I was using several times and what you see there is a hash hastily becoming an array.

    To be quite honest I spent a very long time working on this problem. Not because it was a terribly hard problem to solve but because there was too much room for speculation in the requirements. Finally, I had decided on just presenting a very simplistic example that could be adapted to meet the Anonymous Monk's unspoken requirements. I failed, as you have shown, in reducing it to the basics. This is partly because my mind still hadn't stopped thinking about "what ifs" and also because I am not as great a multi-tasker as I delude myself into believing.

    Cheers - L~R

      Not because it was a terribly hard problem to solve but because there was too much room for speculation in the requirements.

      Heh. I know what you mean. I think there is something here worth meditating on. Your solution to me had all the appearances of a hard sweated solution to stay within the box. He said "sort strings by part of the string", and you worked that angle. But thinking out of the box you can see that in fact its more like "construct a bunch of strings and then use part of them as a sort key" which when you observe that the construction really means joinging the very criteria we want to key by, its a lot clearer that a different approach entirely is better. And this is related to one of my rants of the past, if you stick too closely to a spec, then sometimes solutions become unfeasable. Changing the spec is ok so long as the end result is more or less the same.

      This is partly because my mind still hadn't stopped thinking about "what ifs" and also because I am not as great a multi-tasker as I delude myself into believing.

      Seems a good effort to me. And I know you have a lot on your mind. :-)


      ---
      demerphq

        First they ignore you, then they laugh at you, then they fight you, then you win.
        -- Gandhi