Sort Two columns but only one descending

hi,

I have two columns as seen below. I want to sort column Y in descending order but I want to                                                                      return the the X value of such a Y value as well.

 X         Y

 5        0.9

 3        0.99

 6        0.8

 2        0.85

 etc      etc

 

The solution should look like

X         Y

 3        0.99

 5        0.9

 2        0.85

 6        0.8

 etc      etc

 

Any suggestions?

simple approach

Maple, alas, doesn't not currently have a sort-in-place procedure that operates on rtable (Matrices).  You can accomplish this by converting to a listlist and sorting that. For example

A := <<5,3,6,2>|<0.9,0.99,0.8,0.85>>:
L := convert(A,listlist);
sort(L, (a,b) -> a[2]>b[2]);
Matrix(%);
                                  [3    0.99]
                                  [         ]
                                  [5    0.9 ]
                                  [         ]
                                  [2    0.85]
                                  [         ]
                                  [6    0.8 ]

For large Matrices you might use a modification of sorting-with-attributes:

n := op([1,1],A);
L2 := [seq(setattribute(SFloat(A[i,2]),i), i=1..n)]:
L3 := sort(L2,`>`);
Matrix([seq([A[attributes(L3[i])]], i=1..n)]);
                                  [3    0.99]
                                  [         ]
                                  [5    0.9 ]
                                  [         ]
                                  [2    0.85]
                                  [         ]
                                  [6    0.8 ]

 

Thank you Joe!   As always

Thank you Joe!  

As always solid as a rock!  

I appreciate you taking the time to explain this to me :-)

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
}