Java Sorting Solution
```\nclass Solution {\n    public int[][] allCellsDistOrder(int R, int C, int r0, int c0) {\n        int[][] origin = new int[R * C][2];\n        for (int i = 0
```\nclass Solution {\n    public int[][] allCellsDistOrder(int R, int C, int r0, int c0) {\n        int[][] origin = new int[R * C][2];\n        for (int i = 0; i < R; i++) {\n            for (int j = 0; j < C; j++) {\n                origin[i * C + j] = new int[] {i, j};\n            }\n        }\n\n        Arrays.sort(origin, new Comparator<int[]>(){\n            public int compare(int[] a, int[] b) {\n                return Math.abs(a[0] - r0) + Math.abs(a[1] - c0)\n                        - Math.abs(b[0] - r0) - Math.abs(b[1] - c0);\n            }\n        });\n        return origin;\n    }\n}
[]