From 0522d609312d00a9bb6fa9d1956b52a6f516be77 Mon Sep 17 00:00:00 2001 From: Matej Focko Date: Mon, 22 Jul 2024 10:06:27 +0200 Subject: [PATCH] =?UTF-8?q?java:=20add=20=C2=AB2418.=20Sort=20the=20People?= =?UTF-8?q?=C2=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Matej Focko --- java/sort-the-people.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 java/sort-the-people.java diff --git a/java/sort-the-people.java b/java/sort-the-people.java new file mode 100644 index 0000000..688be9d --- /dev/null +++ b/java/sort-the-people.java @@ -0,0 +1,16 @@ +import java.util.Comparator; + +class Solution { + private record Person(String name, int height) {} + + public String[] sortPeople(String[] names, int[] heights) { + var people = new Person[names.length]; + for (int i = 0; i < names.length; ++i) { + people[i] = new Person(names[i], heights[i]); + } + + Arrays.sort(people, Comparator.comparing(p -> ((Person) p).height).reversed()); + + return Arrays.stream(people).map(p -> p.name).toArray(String[]::new); + } +}