自定义结构体排序
作者:
旷梦
,
2022-02-16 17:50:10
,
所有人可见
,
阅读 220
import java.util.*;
class struct implements Comparable<struct>{
int x,y;
public struct(int x,int y) {
this.x = x;
this.y = y;
}
public int compareTo(struct a) {
if(this.x!=a.x) return this.x-a.x;
else return this.y-a.y;
}
}
public class Main{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
struct a[] = new struct[100];
for(int i=0;i<n;i++) {
int x = sc.nextInt();
int y = sc.nextInt();
a[i] = new struct(x,y);
}
Arrays.sort(a,0,n);
for(int i=0;i<n;i++)
{
System.out.println(a[i].x+" "+a[i].y);
}
}
}