Java PriorityQueue :)
\nclass Solution {\n    public int nearestValidPoint(int x, int y, int[][] points) {\n        \n        //If the distance is same then sort using index else dis
```\nclass Solution {\n    public int nearestValidPoint(int x, int y, int[][] points) {\n        \n        //If the distance is same then sort using index else distance;\n        PriorityQueue<int[]> pq = new PriorityQueue<>((a,b) -> a[1]==b[1] ? a[0]-b[0] : a[1]-b[1]);\n        \n        for(int i=0;i<points.length;i++)\n        {\n            if(points[i][0]==x || points[i][1]==y)\n            {\n                int[] temp = new int[2];\n                temp[0] = i;\n                int distance = Math.abs(points[i][0]-x) + Math.abs(points[i][1]-y);\n                temp[1] = distance;\n                pq.add(temp);\n            }\n        }\n        \n        //NO valid index.\n        if(pq.isEmpty())\n            return -1;\n        else\n        {\n            return pq.poll()[0]; //Returns the index of the fisrt element that has the least manhatt distance.\n        }\n        \n    }\n}\n\n\n```
[]