# Solved: sort a vec

Rust, a multi-paradigm system programming language, built to provide better memory safety while maintaining speed, is the topic of our deep dive today. Specifically, we will discuss in detail how to sort a vector of floating-point numbers (f32) in Rust. The necessity and usefulness of data sorting cannot be overstated as it plays a pivotal role in various applications, ranging from database and statistics to computational theory. Data sorting is essential in computer technology, whether you are filtering your emails or seeking the quickest route home from work.

## Sorting a Vector of f32 in Rust

In Rust, a simple way to sort a vector (Vec) of f32 values is to use the .sort_by() method, together with the total_cmp function from the float_ord crate. This method allows for safe and efficient sorting of f32 values.
Firstly, you have to include the float_ord crate in your project’s `Cargo.toml` file as seen below.

[dependencies]
float_ord = “0.2”

Then, here’s a simple way to sort a Vec:

extern crate float_ord;
use float_ord::FloatOrd;

fn main() {
let mut nums = vec![FloatOrd(-2.2), FloatOrd(-1.1), FloatOrd(0.0), FloatOrd(1.1), FloatOrd(2.2)];
nums.sort();
let nums: Vec = nums.iter().map(|n| n.0).collect();
print!(“{:?}”, nums);
}

Toe to heel, the first line declares the use of the `extern` keyword to import the float_ord crate into our program. The second line then brings the `FloatOrd` struct from the `float_ord` crate into scope. We name our vector `nums` and populate it with numbers.

## Detailed Explanation

The above script begins with importing the float_ord crate, which provides a FloatOrd wrapper that enables total ordering and equality comparisons for floating-point types. It is crucial to use this wrapper, as direct comparisons of floating-point values can spark unpredictable results due to the issue of NaN (Not a Number).

In the main function, we define a mutable Vector nums. The aformentioned numbers are wrapped in the FloatOrd() function, the wrapper provided by the float_ord crate. With these wrappers in place, the nums vector can be sorted via the `.sort()` method.

Then, to transform our vector back to f32, we employ Rust’s powerful iterator function `iter()`, tagged with `map()`, and `collect()`. Here, `map()` applies a function to each element of the Vector.

## Related Functions and Libraries

A related function in Rust is the `sort_unstable()` method which is generally faster than the `sort()` method, but it may not retain the order of equal elements. This may not be a concern when sorting numbers, but can be significant when sorting structs or pairs.

The `sort_by_key()` function is useful when you wish to sort structs or other complex types by a particular attribute. You simply pass in a function that takes an element of the Vector and returns the key you want to sort by.

Rust’s standard library, std::cmp, also provides two key traits, `PartialOrd` and `PartialEq`, for comparing instances of a type for sorting, but these should be used cautiously with floating point numbers due to the matter of NaN.

In a nutshell, the FloatOrd wrapper handles this concern by providing total ordering and equality comparisons for floating-point types in a safe and effective manner.

Related posts: