Skip to content
silentbicycle edited this page Mar 31, 2011 · 1 revision

<x returns the permutation vector for x. It contains the index each value from x would have if it were sorted. This can be used to get a copy of a list, sorted by an arbitrary property. (The algorithm is based on radix sort, and is stable and done in linear time.)

  x:0 5 1 3 4 9 4 7
0 5 1 3 4 9 4 7
  <x          / permutation vector of x
0 2 3 4 6 1 7 5
  x[<x]       / sort x
0 1 3 4 4 5 7 9
  x:(0 5 2; 1 3 8; 4 9 3; 7 7 1; 6 2 6; 8 4 4)
(0 5 2
 1 3 8
 4 9 3
 7 7 1
 6 2 6
 8 4 4)
  x[<x[;1]]   / sort x by second column
(6 2 6
 1 3 8
 8 4 4
 0 5 2
 7 7 1
 4 9 3)
  x:"If you don't have the right abstractions, you can make things artificially difficult. - Joe Armstrong"
"If you don't have the right abstractions, you can make things artificially difficult. - Joe Armstrong"
  x[<x]       / doesn't just work on numeric types
"               ',-.AIJaaaaaaabccccddeeeeffffggghhhhiiiiiiiiklllmmnnnnnoooooorrrrrsssstttttttttuuuvyyy"