Java自定义数组排序

import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
	    Scanner in = new Scanner(System.in);
		int n = in.nextInt();
		Point p[] = new Point[n];
		for(int i = 0; i < n; i++){
			p[i] = new Point();
			p[i].x = in.nextInt();
			p[i].y = in.nextInt();
			}
		//按照优先x从小到大在优先y从小到大排序  
		Arrays.sort(p,new Comparator (){
		    public int compare(Point a, Point b){
			    if(a.x == b.x) return a.y - b.y;
				    return a.x - b.x;
			}
		});
  for(int i = 0; i < n; i++)
	System.out.println(p[i].x+" "+p[i].y);
  in.close();
   }
}
class Point{
	public int x;
	public int y;
}

 

你可能感兴趣的:(Java)