Java, Sort based on distance array
This question description can use some improvement imo, but we just have to compute a distance array and sort all the index pairs based on that. This is likely 
This question description can use some improvement imo, but we just have to compute a distance array and sort all the index pairs based on that. This is likely the most straight forward solution. Another solution would be based on BFS, which I won\'t explore here as other posters have posted them.\n\n```\nclass Solution {\n    public int[][] allCellsDistOrder(int rows, int cols, int rCenter, int cCenter) {\n        int[][] dist = new int[rows][cols];\n        int[][] ans = new int[rows * cols][2];\n        for (int i = 0; i < rows; i++){\n            for (int j = 0; j < cols; j++){\n                dist[i][j] = Math.abs(i - rCenter) + Math.abs(j - cCenter); //dist\n                ans[i * cols + j] = new int[]{i, j}; //add all the index pairs\n            }\n        }\n\n        Arrays.sort(ans, Comparator.comparingInt(o -> dist[o[0]][o[1]])); //sort based on dist\n        return ans;\n    }\n}\n```
['Sorting', 'Java']