rust sort vec of f64
To sort a vector of f64 values in Rust, you can use the sort
method provided by the Vec
type. Here's an example:
fn main() {
let mut numbers = vec![3.14, 1.23, 2.71, 0.99];
numbers.sort_by(|a, b| a.partial_cmp(b).unwrap());
println!("{:?}", numbers);
}
In this example, we create a vector called numbers
containing some floating-point values. We then use the sort_by
method to sort the vector in ascending order. The sort_by
method takes a closure that defines the comparison logic for sorting. In this case, we use the partial_cmp
method to compare the values. Finally, we print the sorted vector using println!
.
The output of this code will be [0.99, 1.23, 2.71, 3.14]
, which represents the sorted vector of f64 values.