java: add «2418. Sort the People»
Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
parent
c911ab1f19
commit
0522d60931
1 changed files with 16 additions and 0 deletions
16
java/sort-the-people.java
Normal file
16
java/sort-the-people.java
Normal file
|
@ -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);
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue