If
[t0=:(|:@}:,.<"0@(".&>)@{:)|: ;:;._2 t
+-+---+---+-----+
|1|aaa|10 |0.2 |
+-+---+---+-----+
|2|bbb|2 |1 |
+-+---+---+-----+
|3|ccc|5 |5 |
+-+---+---+-----+
|4|ddd|22 |0.987|
+-+---+---+-----+
|5|eee|1 |0.987|
+-+---+---+-----+
|6|fff|100|1 |
+-+---+---+-----+
|7|ggg|20 |0.4 |
+-+---+---+-----+
then your problem, if I understand it correctly, boils down to grading the
last column.
(],.~[: <"0@(~.>:@i.]){:"1)(\:{:"1)t0
+-+-+---+---+-----+
|1|3|ccc|5 |5 |
+-+-+---+---+-----+
|2|2|bbb|2 |1 |
+-+-+---+---+-----+
|2|6|fff|100|1 |
+-+-+---+---+-----+
|3|4|ddd|22 |0.987|
+-+-+---+---+-----+
|3|5|eee|1 |0.987|
+-+-+---+---+-----+
|4|7|ggg|20 |0.4 |
+-+-+---+---+-----+
|5|1|aaa|10 |0.2 |
+-+-+---+---+-----+
R.E. BOSS
-----Oorspronkelijk bericht-----
Van: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] Namens June Kim
Verzonden: maandag 8 mei 2006 13:41
Aan: Programming forum
Onderwerp: [Jprogramming] Rank and Sort on a Specific Column
Hello
Firstly, have a look at my quick and dirty code:
t=: 0 : 0
1 aaa 10 0.2
2 bbb 2 1
3 ccc 5 5
4 ddd 22 0.987
5 eee 1 0.987
6 fff 100 1
7 ggg 20 0.4
)
t2=:;:;._2
r=:] i.~ [: ~. \:~
keyf=:[: {:"1 >@(".each)@ {:"1
tt=:keyf (\:@[ { ":each@(<"0)@>:@[EMAIL PROTECTED] ,. ]) ]
sp=: [ , ' ' , ]
line=:>@(<@sp&>/)"1
[EMAIL PROTECTED]@t2 t
1 3 ccc 5 5
2 2 bbb 2 1
2 6 fff 100 1
3 4 ddd 22 0.987
3 5 eee 1 0.987
4 7 ggg 20 0.4
5 1 aaa 10 0.2
Suppose there is a given string table t, and the result should be a
table(shown as in the final result above), descendingly-sorted with
the last number in each line, with the ranks at the first column,
where the rank starts from 1 and when there is a tie the rank doesn't
increase and the next rank starts from current rank plus one.
I am not quite satisfied with my code. Particularly, I don't like that
there are several places doing sorting(\: and keyf, r), hence some
overhead of computation.
Any guidance to improve the code, whether to simplify or optimize, or
both, will be greatly appreciated.
June
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm