Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!
  • Rust

Idiom #240 Sort 2 lists together

Lists a and b have the same length. Apply the same permutation to a and b to have them sorted based on the values of a.

let mut tmp: Vec<_> = a.iter().zip(b).collect();
tmp.as_mut_slice().sort_by_key(|(&x, _y)| x);
let (aa, bb): (Vec<i32>, Vec<i32>) = tmp.into_iter().unzip();
(->> (map vector a b)
     (sort-by first)
     (apply (partial mapv vector)))

this returns [a b]
the last line is from the 2d- transpose solution

New implementation...
< >
programming-idioms.org