public int[][] solution(int[][] data, String ext, int val_ext, String sort_by) {
int extIndex = getIndex(ext);
int sortByIndex = getIndex(sort_by);
List<int[]> filteredList = new ArrayList<>();
for (int[] arr : data) {
if (arr[extIndex] < val_ext) {
filteredList.add(arr);
}
}
Collections.sort(filteredList, (o1, o2) -> {
return Integer.compare(o1[sortByIndex], o2[sortByIndex]);
});
int[][] filteredData = new int[filteredList.size()][];
for (int i = 0; i < filteredList.size(); i++) {
filteredData[i] = filteredList.get(i);
}
return filteredData;
}
private int getIndex(String key) {
switch (key) {
case "code":
return 0;
case "date":
return 1;
case "maximum":
return 2;
case "remain":
return 3;
default:
throw new IllegalArgumentException("Invalid key: " + key);
}
}
출처:
https://school.programmers.co.kr/learn/courses/30/lessons/250121