1
$\begingroup$

I want to sort the array

a = {{{1, 0, 0}, {1, 1, 1}, {0.5, 0.0, 0.5}}, {{-1}, {1}, {-1}}}

in the following way: a[[1]] and a[[2]] have one-to-one correspondence; Sort a[[2]] from small to great value, at the same time, the sequence of a[[1]] will be rearranged accordingly.

In the end, I want to get a new array as

{{{1, 0, 0}, {0.5, 0.0, 0.5}, {1, 1, 1}}, {{-1}, {-1}, {1}}} 

Is there some simple way to realized this in Mathematica?
I appreciate your help!

$\endgroup$
  • $\begingroup$ Have you seen SortBy? $\endgroup$ – Edmund Jul 28 '16 at 17:50
  • 1
    $\begingroup$ Something like this: Transpose@*SortBy[Last]@*Transpose@a? $\endgroup$ – JungHwan Min Jul 28 '16 at 17:53
  • $\begingroup$ Just as another option a = #[[Ordering[Last@a]]] & /@ a $\endgroup$ – N.J.Evans Jul 28 '16 at 18:58
  • 1
    $\begingroup$ You'll need the stable form of SortBy to get your output I believe: SortBy[a\[Transpose], {Last}]\[Transpose] $\endgroup$ – Mr.Wizard Jul 28 '16 at 19:09