Java With TreeMap, easy to understand
\npublic int[][] allCellsDistOrder(int R, int C, int r0, int c0) {\n        TreeMap<Integer, List<int[]>> map = new TreeMap<>();\n        for (int i = 0; i < R;
```\npublic int[][] allCellsDistOrder(int R, int C, int r0, int c0) {\n        TreeMap<Integer, List<int[]>> map = new TreeMap<>();\n        for (int i = 0; i < R; i++) {\n            for (int j = 0; j < C; j++) {\n                int distince = Math.abs(r0 - i) + Math.abs(c0 - j);  //get the distance\n                List<int[]> list = map.getOrDefault(distince, new ArrayList<>());\n                list.add(new int[] {i, j});\n                map.put(distince, list);\n            }\n        }\n        int[][] result = new int[R * C][2]; \n        int i = 0;\n        for (Integer key : map.keySet()) {   //straight forward\n            for (int[] temp : map.get(key)) {\n               result[i++] = temp;\n            }\n        }\n        return result;\n    }\n```
[]