132

What is the currently recommended method for sorting values in a vector?

hippietrail
  • 15,848
  • 18
  • 99
  • 158
Maxim Sloyko
  • 15,176
  • 9
  • 43
  • 49

3 Answers3

125

A mutable slice of elements with a total ordering has a sort method.

Because Vec<T> implements DerefMut<[T]>, you can call this method directly on a vector, so vector.sort() works.

Chris Morgan
  • 86,207
  • 24
  • 208
  • 215
  • What are the requirements for T type? I'm getting an error that says "Vec does not implement any method in scope named 'sort'". I suspect this may be because I did not implement some traits for MyType, I have cmp::PartialEq and cmp::PartialOrd so far. – Maxim Sloyko Nov 10 '14 at 04:22
  • 12
    There's the `sort_by` method too which allows a completely custom predicate. – huon Nov 10 '14 at 04:56
  • 15
    As the docs say, `self.sort()` == `self.sort_by(|a, b| a.cmp(b))`. – Chris Morgan Nov 10 '14 at 22:33
  • 1
    You can just call `.sort()` if the type `T` implements the `cmp::Ord` trait. – Simon Zyx Nov 21 '18 at 08:56
  • 2
    You could also take a look at `sort_unstable` it's bit faster, but could reorder "equal" elements – Bogdan Mart Nov 21 '18 at 15:57
7

To sort a vector v, in most cases v.sort() will be what you need.

If you want to apply a custom ordering rule, you can do that via v.sort_by(). That includes cases where you want to sort values that:

  • don't implement Ord (such as f64, most structs, etc);
  • do implement Ord, but you want to apply a specific non-standard ordering rule.

Also note that sort() and sort_by() use a stable sorting algorithm (i.e., equal elements are not reordered). If you don't need a stable sort, you can use sort_unstable() / sort_unstable_by(), as those are generally a bit faster and use less memory.

at54321
  • 8,726
  • 26
  • 46
-1

While the solutions proposed above can sort vectors of integers I had problems in sorting vectors of floats.

The simplest solution was to use the quickersort crate, which can also sort floats. The quickersort crate can also sort other vectors of any type and also implements methods to sort using comparisons (sort_by).

Following is the Rust code:

extern crate quickersort;
//let's create the vector with the values
let mut vals = Vec::new();
vals.push(31.2);
vals.push(31.2);
vals.push(10.0);
vals.push(100.4);
vals.push(4.1);
quickersort::sort_floats(&mut vals[..]); // sort the vector
Salvatore Cosentino
  • 6,663
  • 6
  • 17
  • 25
  • 17
    You shouldn't need a separate crate just to sort floats - for example, `v.sort_by(|a, b| a.partial_cmp(b).unwrap_or(Ordering::Equal))` should work with floats. (Depending on what you want to do with NaNs in the array, you can write a more careful comparison function.) – user4815162342 Sep 19 '17 at 11:51
  • 5
    *I had problems in sorting vectors of floats* — which is why there already entire questions and answers dedicated to that specific problem (https://stackoverflow.com/q/26489701/155423, https://stackoverflow.com/q/28247990/155423, https://stackoverflow.com/q/37127209/155423). – Shepmaster Sep 19 '17 at 12:34