Skip to content

Commit e374220

Browse files
committed
Recipe Series: Sort Vector #437
* Add simple sorting vector of integers * Add sorting of vector of floats * Add sorting of vector of structs
1 parent 7e59e4e commit e374220

File tree

6 files changed

+115
-0
lines changed

6 files changed

+115
-0
lines changed

src/SUMMARY.md

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
[About](about.md)
55
- [Algorithms](algorithms.md)
66
- [Generate Random Values](algorithms/randomness.md)
7+
- [Sort a Vector](algorithms/sorting.md)
78
- [Command Line](cli.md)
89
- [Argument Parsing](cli/arguments.md)
910
- [Compression](compression.md)

src/algorithms.md

+6
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,18 @@
88
| [Generate random values of a custom type][ex-rand-custom] | [![rand-badge]][rand] | [![cat-science-badge]][cat-science] |
99
| [Create random passwords from a set of alphanumeric characters][ex-rand-passwd] | [![rand-badge]][rand] | [![cat-os-badge]][cat-os] |
1010
| [Create random passwords from a set of user-defined characters][ex-rand-choose] | [![rand-badge]][rand] | [![cat-os-badge]][cat-os] |
11+
| [Sort a Vector of Integers][ex-sort-integers] | [![std-badge]][std] | [![cat-science-badge]][cat-science] |
12+
| [Sort a Vector of Floats][ex-sort-floats] | [![std-badge]][std] | [![cat-science-badge]][cat-science] |
13+
| [Sort a Vector of Structs][ex-sort-structs] | [![std-badge]][std] | [![cat-science-badge]][cat-science] |
1114

1215
[ex-rand]: algorithms/randomness.html#generate-random-numbers
1316
[ex-rand-range]: algorithms/randomness.html#generate-random-numbers-within-a-range
1417
[ex-rand-dist]: algorithms/randomness.html#generate-random-numbers-with-given-distribution
1518
[ex-rand-custom]: algorithms/randomness.html#generate-random-values-of-a-custom-type
1619
[ex-rand-passwd]: algorithms/randomness.html#create-random-passwords-from-a-set-of-alphanumeric-characters
1720
[ex-rand-choose]: algorithms/randomness.html#create-random-passwords-from-a-set-of-user-defined-characters
21+
[ex-sort-integers]: algorithms/sorting.html#sort-a-vector-of-integers
22+
[ex-sort-floats]: algorithms/sorting.html#sort-a-vector-of-floats
23+
[ex-sort-structs]: algorithms/sorting.html#sort-a-vector-of-structs
1824

1925
{{#include links.md}}

src/algorithms/sorting.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Sorting Vectors
2+
3+
{{#include sorting/sort.md}}
4+
{{#include sorting/sort_float.md}}
5+
{{#include sorting/sort_struct.md}}
6+
7+
{{#include ../links.md}}

src/algorithms/sorting/sort.md

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
## Sort a Vector of Integers
2+
3+
[![std-badge]][std] [![cat-science-badge]][cat-science]
4+
5+
This example sorts a Vector of integers via [`vec::sort`]. Alternative would
6+
be to use [`vec::sort_unstable`] which can be faster, but does not preserve
7+
the order of equal elements.
8+
9+
```rust
10+
fn main() {
11+
let mut vec = vec![1, 5, 10, 2, 15];
12+
13+
vec.sort();
14+
15+
assert_eq!(vec, vec![1, 2, 5, 10, 15]);
16+
}
17+
```
18+
19+
[`vec::sort`]: https://doc.rust-lang.org/std/vec/struct.Vec.html#method.sort
20+
[`vec::sort_unstable`]: https://doc.rust-lang.org/std/vec/struct.Vec.html#method.sort_unstable

src/algorithms/sorting/sort_float.md

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
## Sort a Vector of Floats
2+
3+
[![std-badge]][std] [![cat-science-badge]][cat-science]
4+
5+
A Vector of f32 or f64 can be sorted with [`vec::sort_by`] and [`PartialOrd::partial_cmp`].
6+
7+
```rust
8+
fn main() {
9+
let mut vec = vec![1.1, 1.15, 5.5, 1.123, 2.0];
10+
11+
vec.sort_by(|a, b| a.partial_cmp(b).unwrap());
12+
13+
assert_eq!(vec, vec![1.1, 1.123, 1.15, 2.0, 5.5]);
14+
}
15+
```
16+
17+
[`vec::sort_by`]: https://doc.rust-lang.org/std/primitive.slice.html#method.sort_by
18+
[`PartialOrd::partial_cmp`]: https://doc.rust-lang.org/std/cmp/trait.PartialOrd.html#tymethod.partial_cmp

src/algorithms/sorting/sort_struct.md

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
## Sort a Vector of Structs
2+
3+
[![std-badge]][std] [![cat-science-badge]][cat-science]
4+
5+
Sorts a Vector of Person structs with properties `name` and `age` by its natural
6+
order (By name and age). In order to make Person sortable you need four traits [`Eq`],
7+
[`PartialEq`], [`Ord`] and [`PartialOrd`]. These traits can be siply derived.
8+
You can also provide a custom comparator function using a [`vec:sort_by`] method and sort only by age.
9+
10+
```rust
11+
#[derive(Debug, Eq, Ord, PartialEq, PartialOrd)]
12+
struct Person {
13+
name: String,
14+
age: u32
15+
}
16+
17+
impl Person {
18+
pub fn new(name: String, age: u32) -> Self {
19+
Person {
20+
name,
21+
age
22+
}
23+
}
24+
}
25+
26+
fn main() {
27+
let mut people = vec![
28+
Person::new("Zoe".to_string(), 25),
29+
Person::new("Al".to_string(), 60),
30+
Person::new("John".to_string(), 1),
31+
];
32+
33+
// Sort people by derived natural order (Name and age)
34+
people.sort();
35+
36+
assert_eq!(
37+
people,
38+
vec![
39+
Person::new("Al".to_string(), 60),
40+
Person::new("John".to_string(), 1),
41+
Person::new("Zoe".to_string(), 25),
42+
]);
43+
44+
// Sort people by age
45+
people.sort_by(|a, b| b.age.cmp(&a.age));
46+
47+
assert_eq!(
48+
people,
49+
vec![
50+
Person::new("Al".to_string(), 60),
51+
Person::new("Zoe".to_string(), 25),
52+
Person::new("John".to_string(), 1),
53+
]);
54+
55+
}
56+
57+
```
58+
59+
[`Eq`]: https://doc.rust-lang.org/std/cmp/trait.Eq.html
60+
[`PartialEq`]: https://doc.rust-lang.org/std/cmp/trait.PartialEq.html
61+
[`Ord`]: https://doc.rust-lang.org/std/cmp/trait.Ord.html
62+
[`PartialOrd`]: https://doc.rust-lang.org/std/cmp/trait.PartialOrd.html
63+
[`vec:sort_by`]: https://doc.rust-lang.org/std/vec/struct.Vec.html#method.sort_by

0 commit comments

Comments
 (0)