As I said before, if the data is numeric, for each descending column,
multiply by _1 and for each ascending column, multiply by 1, then apply /:
to the result.  This induces a "control array" having the same shape as a
major cell of the argument, with a _1 for descending and a 1 for ascending.

What if the data is not all numeric?  You can convert it to an
order-equivalent numeric array by assigning to each atom an ordinal (an
integer) which bears the same ordering relationship with the original atom,
then solve numeric problem:

   ord =: /:~@, i.!.0 ]

That is, the ordinals obtain as the indices of the original array in the
sorted list of the ravelled elements, using exact comparisons.

For example:

   x0=: <"0 ?19 $ 4
   x1=: (?19$2){'alpha';'beta'
   x2=: <"0 (?19$3){'abc'
   x3=: (?19$3){'able' ;'baker' ; 'charlie'; 'echo'

   x=: x0,.x1,.x2,.x3

   ord x
 9 60 26 52
11 45 19 38
11 60 26 38
11 45 26 52
 6 60 26 72
 6 60 19 52
 0 60 33 72
 0 60 26 52
 0 60 33 38
 0 60 19 38
11 45 19 52
11 45 19 52
 6 45 33 38
 0 60 26 72
 0 45 33 52
11 60 26 38
 9 60 19 52
11 60 33 72
11 45 19 38

Suppose x is to be sorted ascending in columns 0 and 2 and descending in
columns 1 and 3?  The control array is 1 _1 1 _1, and:

   x                       (/: 1 _1 1 _1*"1 ord x) {x
┌─┬─────┬─┬───────┐     ┌─┬─────┬─┬───────┐
│2│beta │b│baker  │     │0│beta │a│able   │
├─┼─────┼─┼───────┤     ├─┼─────┼─┼───────┤
│3│alpha│a│able   │     │0│beta │b│charlie│
├─┼─────┼─┼───────┤     ├─┼─────┼─┼───────┤
│3│beta │b│able   │     │0│beta │b│baker  │
├─┼─────┼─┼───────┤     ├─┼─────┼─┼───────┤
│3│alpha│b│baker  │     │0│beta │c│charlie│
├─┼─────┼─┼───────┤     ├─┼─────┼─┼───────┤
│1│beta │b│charlie│     │0│beta │c│able   │
├─┼─────┼─┼───────┤     ├─┼─────┼─┼───────┤
│1│beta │a│baker  │     │0│alpha│c│baker  │
├─┼─────┼─┼───────┤     ├─┼─────┼─┼───────┤
│0│beta │c│charlie│     │1│beta │a│baker  │
├─┼─────┼─┼───────┤     ├─┼─────┼─┼───────┤
│0│beta │b│baker  │     │1│beta │b│charlie│
├─┼─────┼─┼───────┤     ├─┼─────┼─┼───────┤
│0│beta │c│able   │     │1│alpha│c│able   │
├─┼─────┼─┼───────┤     ├─┼─────┼─┼───────┤
│0│beta │a│able   │     │2│beta │a│baker  │
├─┼─────┼─┼───────┤     ├─┼─────┼─┼───────┤
│3│alpha│a│baker  │     │2│beta │b│baker  │
├─┼─────┼─┼───────┤     ├─┼─────┼─┼───────┤
│3│alpha│a│baker  │     │3│beta │b│able   │
├─┼─────┼─┼───────┤     ├─┼─────┼─┼───────┤
│1│alpha│c│able   │     │3│beta │b│able   │
├─┼─────┼─┼───────┤     ├─┼─────┼─┼───────┤
│0│beta │b│charlie│     │3│beta │c│charlie│
├─┼─────┼─┼───────┤     ├─┼─────┼─┼───────┤
│0│alpha│c│baker  │     │3│alpha│a│baker  │
├─┼─────┼─┼───────┤     ├─┼─────┼─┼───────┤
│3│beta │b│able   │     │3│alpha│a│baker  │
├─┼─────┼─┼───────┤     ├─┼─────┼─┼───────┤
│2│beta │a│baker  │     │3│alpha│a│able   │
├─┼─────┼─┼───────┤     ├─┼─────┼─┼───────┤
│3│beta │c│charlie│     │3│alpha│a│able   │
├─┼─────┼─┼───────┤     ├─┼─────┼─┼───────┤
│3│alpha│a│able   │     │3│alpha│b│baker  │
└─┴─────┴─┴───────┘     └─┴─────┴─┴───────┘


On Sat, Jan 19, 2019 at 8:40 AM Roger Hui <rogerhui.can...@gmail.com> wrote:

> If the keys are numeric you can multiply the ascending column by 1 and the
> descending one by _1, and then apply /:  .
>
>    x=: 5 2$7 4 8 2 8 4 7 2 8 3
>    x /: x*"1 ]_1 1
> 8 2
> 8 3
> 8 4
> 7 2
> 7 4
>    x /: x*"1 ]1 _1
> 7 4
> 7 2
> 8 4
> 8 3
> 8 2
>
>
> On Sat, Jan 19, 2019 at 8:18 AM Jimmy Gauvin <jimmy.gau...@gmail.com>
> wrote:
>
>> Hi,
>>
>> I need to sort on two keys, one descending the other asscending.
>> (J takes care were ascending or descending
>>
>> Is there a better way to solve this ?
>>
>>    ]t=:5 2$7 4 8 2 8 4 7 2 8 3
>> 7 4
>> 8 2
>> 8 4
>> 7 2
>> 8 3
>>    ]tt=: t {~ /:1{"1 t
>> 8 2
>> 7 2
>> 8 3
>> 7 4
>> 8 4
>>    ]tt {~ \:0{"1 tt
>> 8 2
>> 8 3
>> 8 4
>> 7 2
>> 7 4
>>
>> Thanks,
>>
>> Jimmy
>> ----------------------------------------------------------------------
>> For information about J forums see http://www.jsoftware.com/forums.htm
>
>
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to