Avatar ·

How to sort a vector in Rust: ascending, descending, and with custom logic

Hi! I just started learning Rust and ran into a problem: I can't figure out how to properly sort a vector. I'm used to Python where you can just call `sorted()`, but here it's different. Let's say I have a vector of numbers: `let mut numbers = vec![5, 2, 8, 1, 9];`. I want to sort it in ascending order to get `[1, 2, 5, 8, 9]`. Later, I might need to sort it in descending order. I tried writing something like `numbers.sort()` and it seems to work, but I'm not sure if that's correct. I also have a vector of strings: `let mut words = vec!["banana", "apple", "cherry", "date"];`. How do I sort it in alphabetical order? And what if I want to sort by string length (for example, from shortest to longest)? And finally, I have a struct `Person` with fields `name`