Skip to main content
added 593 characters in body
Source Link
Roger Harris
  • 1.5k
  • 1
  • 13
  • 20

Imagine I sort the elements of an array $Q$ by some means. Perhaps I sort strictly by the elements in the first column, perhaps I use SortBy[...] to sequentially sort by the elements in successive columns, etc. Here, regardless of what I do, the result of this sort will be some one-to-one mapping from items in $Q$ to some items in the sorted array, $Q^*$.

Can I somehow retain or reuse this particular one-to-one mapping function on some other array with the same dimensions as $Q$? Very specifically, I mean that if we move element $q_i \in Q$ at position $(a_1, a_2, ...)$ in $Q$ to some new position $(b_1, b_2, ...)$ in $Q^*$, then we corresponding move the same element $h_i \in H$ from $(a_1, a_2, ...)$ in $H$ to $(b_1, b_2, ...)$ in $H^*$. And we do this regardless of the identity of $h_i$, i.e. we're not repeating the sort.


To provide a specific example of where I'm getting in trouble, I'd like to sort the following list by the first column, then the second (with this priority):

data = {{4, 5}, {6, 1}, {11, 87}, {24, 52}, {90, 4}, {4, 4}};

 In[...]:= SortBy[data, {#[[1]], #[[2]]} &]
 Out[...]:= {{4, 4}, {4, 5}, {6, 1}, {11, 87}, {24, 52}, {90, 4}}

However:

  In[...]:= Ordering[SortBy[data, {#[[1]], #[[2]]} &]]
  Out[...]:= {1, 2, 3, 4, 5, 6} 

What I wanted from Ordering[...] was:

  Out[...]:= {6, 1, 2, 3, 4, 5}

How can I make the above work?

Imagine I sort the elements of an array $Q$ by some means. Perhaps I sort strictly by the elements in the first column, perhaps I use SortBy[...] to sequentially sort by the elements in successive columns, etc. Here, regardless of what I do, the result of this sort will be some one-to-one mapping from items in $Q$ to some items in the sorted array, $Q^*$.

Can I somehow retain or reuse this particular one-to-one mapping function on some other array with the same dimensions as $Q$? Very specifically, I mean that if we move element $q_i \in Q$ at position $(a_1, a_2, ...)$ in $Q$ to some new position $(b_1, b_2, ...)$ in $Q^*$, then we corresponding move the same element $h_i \in H$ from $(a_1, a_2, ...)$ in $H$ to $(b_1, b_2, ...)$ in $H^*$. And we do this regardless of the identity of $h_i$, i.e. we're not repeating the sort.

Imagine I sort the elements of an array $Q$ by some means. Perhaps I sort strictly by the elements in the first column, perhaps I use SortBy[...] to sequentially sort by the elements in successive columns, etc. Here, regardless of what I do, the result of this sort will be some one-to-one mapping from items in $Q$ to some items in the sorted array, $Q^*$.

Can I somehow retain or reuse this particular one-to-one mapping function on some other array with the same dimensions as $Q$? Very specifically, I mean that if we move element $q_i \in Q$ at position $(a_1, a_2, ...)$ in $Q$ to some new position $(b_1, b_2, ...)$ in $Q^*$, then we corresponding move the same element $h_i \in H$ from $(a_1, a_2, ...)$ in $H$ to $(b_1, b_2, ...)$ in $H^*$. And we do this regardless of the identity of $h_i$, i.e. we're not repeating the sort.


To provide a specific example of where I'm getting in trouble, I'd like to sort the following list by the first column, then the second (with this priority):

data = {{4, 5}, {6, 1}, {11, 87}, {24, 52}, {90, 4}, {4, 4}};

 In[...]:= SortBy[data, {#[[1]], #[[2]]} &]
 Out[...]:= {{4, 4}, {4, 5}, {6, 1}, {11, 87}, {24, 52}, {90, 4}}

However:

  In[...]:= Ordering[SortBy[data, {#[[1]], #[[2]]} &]]
  Out[...]:= {1, 2, 3, 4, 5, 6} 

What I wanted from Ordering[...] was:

  Out[...]:= {6, 1, 2, 3, 4, 5}

How can I make the above work?

Source Link
Roger Harris
  • 1.5k
  • 1
  • 13
  • 20

Retaining and reusing a one-to-one mapping from a sort

Imagine I sort the elements of an array $Q$ by some means. Perhaps I sort strictly by the elements in the first column, perhaps I use SortBy[...] to sequentially sort by the elements in successive columns, etc. Here, regardless of what I do, the result of this sort will be some one-to-one mapping from items in $Q$ to some items in the sorted array, $Q^*$.

Can I somehow retain or reuse this particular one-to-one mapping function on some other array with the same dimensions as $Q$? Very specifically, I mean that if we move element $q_i \in Q$ at position $(a_1, a_2, ...)$ in $Q$ to some new position $(b_1, b_2, ...)$ in $Q^*$, then we corresponding move the same element $h_i \in H$ from $(a_1, a_2, ...)$ in $H$ to $(b_1, b_2, ...)$ in $H^*$. And we do this regardless of the identity of $h_i$, i.e. we're not repeating the sort.