Sort the People

easy array sorting hash map

Problem

You are given an array of names and an array of heights, where the person at index i is named names[i] and stands heights[i] tall. Every height is distinct. Return the names ordered so that the tallest person comes first and the shortest comes last (descending by height).

Inputnames = ["Mary", "John", "Emma"], heights = [180, 165, 170]
Output["Mary", "Emma", "John"]
Sorting heights from tallest to shortest gives 180, 170, 165 — that is Mary, then Emma, then John.

def sort_people(names, heights):
    people = list(zip(heights, names))
    people.sort(reverse=True)
    result = []
    for height, name in people:
        result.append(name)
    return result
function sortPeople(names, heights) {
  const people = names.map((name, i) => [heights[i], name]);
  people.sort((a, b) => b[0] - a[0]);
  const result = [];
  for (const [height, name] of people) {
    result.push(name);
  }
  return result;
}
class Solution {
    public String[] sortPeople(String[] names, int[] heights) {
        Integer[] idx = new Integer[names.length];
        for (int i = 0; i < names.length; i++) idx[i] = i;
        Arrays.sort(idx, (a, b) -> heights[b] - heights[a]);
        String[] result = new String[names.length];
        for (int i = 0; i < idx.length; i++) result[i] = names[idx[i]];
        return result;
    }
}
vector<string> sortPeople(vector<string>& names, vector<int>& heights) {
    vector<pair<int, string>> people;
    for (int i = 0; i < (int)names.size(); i++) people.push_back({heights[i], names[i]});
    sort(people.rbegin(), people.rend());
    vector<string> result;
    for (auto& p : people) result.push_back(p.second);
    return result;
}
Time: O(n log n) Space: O(n)