#2418
Easy Algorithms Sort the people
Array Hash Table String Sorting
84.8% acceptance
Feb 25, 2026
1857
40
You are given an array of strings names, and an array heights that consists of distinct
positive integers. Both arrays are of length n.
For each index i, names[i] and heights[i] denote the name and height of the ith person.
Return names sorted in descending order by the people's heights.
Solution
Rust
Time O(n log n)
Space O(1)
impl Solution {
pub fn sort_people(names: Vec<String>, heights: Vec<i32>) -> Vec<String> {
let mut pairs: Vec<(i32, String)> = heights.into_iter().zip(names.into_iter()).collect();
pairs.sort_by(|a, b| b.0.cmp(&a.0));
pairs.into_iter().map(|(_, name)| name).collect()
}
}