Task Description: you are given an unordered list of comma-separated strings with the order of values corresponding to the following: 0: unique identifier 1: x coordinates of the top left corner of a box 2: y coordinates of the top left corner of a box 3: height of a box 4: width of a box
Create a function that will return a sorted list of valid comma separated strings. sort first by the y
coordinate, if y
coordinates are equal, then sort by x
coordinate. Complete the 'NavigationSort
' function below. The function is expected to return a STRING_ARRAY
.
The function accepts STRING_ARRAY boxArray as parameter.
public static List<String> NavigationSort(List<String> boxArray) {
// write your code here
return NavigationSort;
}
}
`
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
int boxArrayCount = Integer.parseInt(bufferedReader.readLine().trim());
List<String> boxArray = IntStream.range(0, boxArrayCount).mapToObj(i -> {
try {
return bufferedReader.readLine();
} catch (IOException ex) {
throw new RuntimeException(ex);
}
}).collect(toList());
List<String> result = Result.NavigationSort(boxArray);
bufferedWriter.write(result.stream().collect(joining("\n")) + "\n");
bufferedReader.close();
bufferedWriter.close();
}
}
You just need to write an appropriate
Comparator
.This is just the implementation of the
navigationSort
method. It's not clear how to obtain the source list but if you need to read it from a file, then simply use method readAllLInes in classjava.nio.file.Files
. Also note that the below code assumes that the method parameter, i.e.boxArray
, contains valid data according to the problem description.