moin Dimitar,

Quoting [email protected]:
>
...

> When replying, please edit your Subject line so it is more specific
>> than "Re: Contents of Perldl digest..."
>>
> ...

On Mon, Jun 2, 2014 at 3:46 AM, <[email protected]> wrote:

> Thank you Bryan and Chris,
> i am sorry i did not make it clear in my first message how i want to sort.
>
> I want to sort 2 times. First by columns 0 and 1(numerically) and after by
> column 2(lexicographically).
>

I guess I still don't understand -- do you really need 2 separate sort
orders (resulting in 2 separate output piddles), or do you want to use
primary and secondary sort keys in a single sort operation?  e.g. do you
want

  @in = ([4,'a'],[1,'c'],[1,'b']);

  ##-- 2 sorts
  @out1 = sort {$a->[0] <=> $b->[0]} @in;
  @out2 = sort {$a->[1} cmp $b->[1]} @in;

  ##-- single sort with multiple keys
  @out = sort {$a->[0]<=>$b->[0] || $a->[1] cmp $b->[1]} @in;

... if (as i assume) it's really the single-sort-with-multiple-keys you're
after, then this is exactly what qsortvec() ought to do, provided that you
represent all your values -- be they numeric or character values -- with a
sufficiently general datatype (e.g. PDL::long); examples.  another example:

  $p = pdl(byte, [[4,5,ord('a'),6],[1,2,ord('c'),3],[1,2,ord('b'),9]]);
  print $p, $p->qsortvec;

prints:
[
 [ 4  5 97  6]
 [ 1  2 99  3]
 [ 1  2 98  9]
]
[
 [ 1  2 98  9]
 [ 1  2 99  3]
 [ 4  5 97  6]
]

... this works as long as the numeric values of your character symbols
correspond to the lexicographic sort order you want (i.e. works well for
ASCII and pretty much nothing else) -- if you need "real" locale-sensitive
sorting, you'll probably need to construct an extra key-piddle, get the
sort order using qsortveci() on that, and then back-translate to your
original piddle with dice_axis().

if you're really after the 2-sort solution, then you just need to slice
your source piddle appropriately before sorting; and of course take care
that your character values reflect the lexicographic order you want.


> As i understood qsort sorts only by first column(0).
>

yes.  that's why there's qsortvec().  as its name implies, it sorts vectors
(0th dimension: vector.  it can generalize to n-dimensions if you reorder()
and clump() your input appropriately.   Using PDL::Char is tricky --
Chris's discovery of $p->numeric() can combine with clump() here to get you
a piddle of character values like my ord() trick:

 $p_char = PDL::Char->new(@a=([1,2,'c',3],[4,5,'a',6],[7,8,'b',9]));
 $p_num_sorted = $pchar->numeric->clump(2)->qsortvec;

... I don't really know what the singleton 0th dimension is doing in
PDL::Char piddles, but it's easy enough to get rid of with clump().

marmosets,
  Bryan


>
> Regards
> Dimitar
>
>
> Date: Fri, 30 May 2014 15:20:01 +0800
>> From: [email protected]
>> Subject: [Perldl] question about sort
>>
>> hi guys,
>> just a brief question about sorting a piddle.
>>
>> i have a piddle of this kind:
>>     $p=PDL::Char->new(@a=([1,2,'c',3],[4,5,'a',6],[7,8,'b',9]));
>>
>> And i would like to sort it by columns for example columns 2 and 3.
>> But the only sorting i found in PDL was qsort and qsortvec but they
>> dont do what i want. I searched the net but could not find any info on
>> this matter.
>>
>> I was thinking to convert it to regular perl array so i can sort as i
>> want but this is not very effective cos my actual piddle is large.
>>
>> Can you please help me in this matter? Any tips of info is appreciated
>>
>> Thank you
>> Dimitar
>>
>> ------------------------------
>> Date: Fri, 30 May 2014 10:23:44 +0200
>> From: Bryan Jurish <[email protected]>
>> Subject: Re: [Perldl] question about sort
>>
>> moin Dimitar,
>>
>> I don't use PDL::Char, but I think you ought to be able to do what you
>> want
>> using qsortveci(); something like:
>>
>>   $p = pdl(byte, [map {[map {ord $_} @$_]}
>> [1,2,'c',3],[4,5,'a',6],[7,8,'b',9]]);  # use a "vanilla" piddle ...
>>   $keys         = $p->slice("1:2,");
>>   $p_sorted = $p->dice_axis(1, $keys->qsortveci);
>>
>> ... PDL::Char seems to behave a bit strangely here; but maybe this can get
>> you on your way...
>>
>> marmosets,
>>   Bryan
>> ------------------------------
>> Date: Fri, 30 May 2014 08:44:48 -0400
>> From: Chris Marshall <[email protected]>
>> Subject: Re: [Perldl] question about sort
>>
>> You don't say what type of comparison function you want for the sort.  The
>> qsort, qsortvec and qsortveci all use the numeric value to sort.  Looking
>> at the PDL::Char source it seems there is an undocumented method to return
>> the PDL::Char piddle as a standard PDL piddle.  This may be of use.
>>
>>
>> pdl> use PDL::Char
>>
>>>
>>> pdl>  $p=PDL::Char->new(@a=([1,2,'c',3],[4,5,'a',6],[7,8,'b',9]));
>>>
>>> pdl> p $p
>>> [
>>>  [ '1' '2' 'c' '3'  ]
>>>  [ '4' '5' 'a' '6'  ]
>>>  [ '7' '8' 'b' '9'  ]
>>> ]
>>>
>>>
>>> pdl> p $p->numeric
>>>
>>> [
>>>  [
>>>   [49]
>>>   [50]
>>>   [99]
>>>   [51]
>>>  ]
>>>  [
>>>   [52]
>>>   [53]
>>>   [97]
>>>   [54]
>>>  ]
>>>  [
>>>   [55]
>>>   [56]
>>>   [98]
>>>   [57]
>>>  ]
>>> ]
>>>
>>>
>>>
>>>  Hope this helps.  --Chris
>>
>
-- 
Bryan Jurish                           "There is *always* one more bug."
[email protected]         -Lubarsky's Law of Cybernetic Entomology
_______________________________________________
Perldl mailing list
[email protected]
http://mailman.jach.hawaii.edu/mailman/listinfo/perldl

Reply via email to