Question
What is the currently recommended way to sort values stored in a Rust vector?
For example, if I have a Vec<T>, what methods should I use to sort it in ascending order, descending order, or with custom sorting logic?
Short Answer
By the end of this page, you will understand how to sort a Vec in Rust using the standard library. You will learn when to use sort(), sort_unstable(), and comparator-based methods such as sort_by() and sort_by_key(), along with common mistakes and practical examples.
Concept
Sorting in Rust means rearranging the items in a collection, usually a Vec<T>, into a specific order.
The standard library provides built-in sorting methods directly on slices, and since a vector can be viewed as a mutable slice, you can call these methods on Vec<T> as well.
The most common sorting methods are:
sort()for a stable sort in natural ordersort_unstable()for a faster unstable sort when order of equal elements does not mattersort_by()for custom comparison logicsort_by_key()for sorting by a derived key
Why this matters:
- Sorted data is easier to search, display, and process.
- Many algorithms assume sorted input.
- Real applications often sort users, products, logs, dates, or scores.
In Rust, the recommended method depends on what you need:
- Use
sort()when you want a simple ascending sort and stability matters. - Use
sort_unstable()when you want speed and do not care about preserving the relative order of equal items. - Use
sort_by()orsort_by_key()when sorting structs or using custom rules.
For basic values like integers or strings, sort() is usually the clearest beginner-friendly choice.
Mental Model
Think of a vector like a stack of index cards.
sort()reorganizes the cards from smallest to largest while keeping tied cards in their original relative order.sort_unstable()also organizes the cards, but if two cards are equal, it may shuffle those tied cards around.sort_by_key()is like saying, "Sort these cards by the number written in the corner," even if the full card contains more information.sort_by()is like giving a human sorter a custom rulebook for how to compare any two cards.
So the main question is not just "Can Rust sort this?" but also "What comparison rule should Rust use, and do I care about stability?"
Syntax and Examples
Basic ascending sort
fn main() {
let mut numbers = vec![4, 2, 5, 1, 3];
numbers.sort();
println!("{:?}", numbers);
}
Output:
[1, 2, 3, 4, 5]
sort() sorts the vector in place. That means it changes the original vector instead of creating a new one.
Descending sort
fn main() {
let mut numbers = vec![4, 2, 5, 1, 3];
numbers.sort_by(|a, b| b.cmp(a));
println!(, numbers);
}
Step by Step Execution
Consider this example:
fn main() {
let mut numbers = vec![3, 1, 2];
numbers.sort();
println!("{:?}", numbers);
}
Step by step:
-
let mut numbers = vec![3, 1, 2];- A mutable vector is created.
- The contents are
[3, 1, 2].
-
numbers.sort();- Rust sorts the vector in ascending order.
- It compares elements using their natural ordering.
- After sorting, the vector becomes
[1, 2, 3].
-
println!("{:?}", numbers);- The sorted vector is printed.
- Output is
[1, 2, 3].
Now a custom example:
fn () {
= [, , ];
numbers.(|a, b| b.(a));
(, numbers);
}
Real World Use Cases
Sorting vectors is common in real Rust programs.
API and web apps
- Sort products by price before returning JSON.
- Sort users by signup date.
- Sort comments by score or time.
Command-line tools
- Sort file names alphabetically.
- Sort log entries by timestamp.
- Sort benchmark results from fastest to slowest.
Data processing
- Sort numeric measurements before analysis.
- Sort records by a key before grouping.
- Sort words for deterministic output.
Game and simulation code
- Sort leaderboard scores.
- Sort entities by render order.
- Sort events by priority.
In all of these cases, the choice between stable and unstable sort depends on whether equal items must keep their original order.
Real Codebase Usage
In real projects, developers often use sorting together with common patterns.
Sorting simple values
numbers.sort();
Used for integers, strings, and other types that implement Ord.
Sorting by a struct field
users.sort_by_key(|u| u.age);
This is very common for business data such as users, orders, or events.
Reverse order
use std::cmp::Reverse;
fn main() {
let mut numbers = vec![4, 2, 5, 1, 3];
numbers.sort_by_key(|&n| Reverse(n));
println!("{:?}", numbers);
}
Reverse is a clean pattern for descending sorts.
Stable sorting when ties matter
If you sort by one field and want equal values to keep their original order, use sort() or .
Common Mistakes
1. Forgetting the vector must be mutable
Broken code:
fn main() {
let numbers = vec![3, 1, 2];
numbers.sort();
}
Why it fails:
sort()modifies the vector in place.- The vector must be declared with
mut.
Fixed code:
fn main() {
let mut numbers = vec![3, 1, 2];
numbers.sort();
}
2. Expecting sort() to return a new vector
Broken idea:
fn main() {
let mut numbers = vec![, , ];
= numbers.();
(, sorted);
}
Comparisons
| Method | Stable | Custom logic | Best use case |
|---|---|---|---|
sort() | Yes | No | Simple ascending sort for types implementing Ord |
sort_unstable() | No | No | Faster sort when order of equal elements does not matter |
sort_by() | Yes | Yes | Complex custom comparison |
sort_by_key() | Yes | Yes, by key | Sorting structs or values by one field or derived key |
sort_unstable_by() | No |
Cheat Sheet
// Ascending sort
vec.sort();
// Descending sort
vec.sort_by(|a, b| b.cmp(a));
// Fast unstable ascending sort
vec.sort_unstable();
// Sort by a field
vec.sort_by_key(|item| item.field);
// Sort by a field in descending order
use std::cmp::Reverse;
vec.sort_by_key(|item| Reverse(item.field));
// Custom comparison
vec.sort_by(|a, b| a.some_field.cmp(&b.some_field));
Rules to remember
- The vector must be mutable.
- Sorting happens in place.
sort()requires the element type to implementOrd.- Use
sort_by()orsort_by_key()for custom sorting. - Use
sort_unstable()when stability is not required.
Good defaults
- Beginners: start with
sort() - Sorting structs by one field: use
sort_by_key()
FAQ
How do I sort a vector in ascending order in Rust?
Use sort():
let mut numbers = vec![3, 1, 2];
numbers.sort();
How do I sort a vector in descending order in Rust?
Use sort_by() with reversed comparison:
numbers.sort_by(|a, b| b.cmp(a));
What is the difference between sort() and sort_unstable() in Rust?
sort() is stable and preserves the order of equal elements. sort_unstable() may reorder equal elements but can be faster.
Can I sort a vector of structs in Rust?
Yes. A common approach is sort_by_key():
users.sort_by_key(|u| u.age);
Does create a new vector?
Mini Project
Description
Build a small Rust program that sorts a list of products in different ways. This demonstrates basic vector sorting, descending sorting, and sorting structs by a field, which are all common tasks in real applications.
Goal
Create a Rust program that sorts numbers and products using sort(), sort_by(), and sort_by_key().
Requirements
- Create a vector of integers and sort it in ascending order.
- Sort the same integer vector in descending order.
- Create a
Productstruct withnameandpricefields. - Store several products in a vector.
- Sort the products by price and print the result.
Keep learning
Related questions
Accessing Cargo Package Metadata in Rust
Learn how to read Cargo package metadata like version, name, and authors in Rust using compile-time environment macros.
Associated Types vs Generic Type Parameters in Rust: When to Use Each
Learn when to use associated types vs generic parameters in Rust traits, with clear rules, examples, and practical API design advice.
Can a Struct Extend Another Struct in Rust? Composition vs Inheritance
Learn how Rust handles struct reuse without inheritance, using composition, traits, and wrapper structs with practical examples.