java (结构体排序,多元素排序)

两种写法,代码里有详细解释:

第一种: Comparable

int compareTo(Object o); 

import java.util.*;
public class Main {
	static Scanner cin = new Scanner(System.in);
	static class hh implements Comparable<hh>{
		int x,y;
		public hh(int x,int y) {
			this.x=x;
			this.y=y;
		}
//		public int compareTo(hh a) {
//			if(this.x-a.x!=0) return this.x-a.x;//按x升序
//			else return this.y-a.y;//如果x相等,按y升序
//		}
//		public int compareTo(hh a) {
//			if(this.x-a.x!=0) return this.x-a.x;//按x升序
//			else return a.y-this.y;//如果x相等,按y降序
//		}
//		public int compareTo(hh a) {
//			if(this.x-a.x!=0) return a.x-this.x;//按x降序
//			else return a.y-this.y;//如果x相等,按y降序
//		}
		public int compareTo(hh a) {
			if(this.x-a.x!=0) return a.x-this.x;//按x降序
			else return this.y-a.y;//如果x相等,按y升序
		}
	}
    public static void main(String[] args){
    	int n=cin.nextInt();
    	hh a [] = new hh [55];
    	for (int i = 0; i < n;i++) {
//    		a[i]=new hh();//这样写不能写构造函数~~
//    		a[i].x=cin.nextInt();
//    		a[i].y=cin.nextInt();
    		a[i]=new hh (cin.nextInt(),cin.nextInt());//这样需要写构造函数
    	}
    	Arrays.sort(a, 0,n);
    	for (int i = 0; i < n;i++) {
    		System.out.println(a[i].x+" "+a[i].y);
    	}
    }
}
/*输入样例
 5
 1 2
 2 3
 1 4
 2 5
 5 5
 */
/*输出样例 按x升序,x相等按y升序
1 2
1 4
2 3
2 5
5 5
*/
/*输出样例 按x升序,x相等按y降序
1 4
1 2
2 5
2 3
5 5
*/
/*输出样例 按x降序,x相等按y降序
5 5
2 5
2 3
1 4
1 2
*/
/*输出样例 按x降序,x相等按y升序
5 5
2 3
2 5
1 2
1 4
*/
 

第二种:Comparator

 int compare(Object o1, Object o2);

这种写法再分为两种:

one:带new的写法,看解释

two:不带new的写法,看解释

one:

import java.util.*;
public class Main {
	static Scanner cin = new Scanner(System.in);
	static class hh {
		int x,y;
		public hh(int x,int y) {
			this.x=x;
			this.y=y;
		}
	}
//	static class cmp implements Comparator<hh>{
//		public int compare(hh a,hh b) {
//			if(a.x!=b.x) return a.x-b.x;//按x升序
//			else return a.y-b.y;//如果x相等,按y升序
//		}
//	}
//	static class cmp implements Comparator<hh>{
//		public int compare(hh a,hh b) {
//			if(a.x!=b.x) return a.x-b.x;//按x升序
//			else return b.y-a.y;//如果x相等,按y降序
//		}
//	}
//	static class cmp implements Comparator<hh>{
//		public int compare(hh a,hh b) {
//			if(a.x!=b.x) return b.x-a.x;//按x降序
//			else return b.y-a.y;//如果x相等,按y降序
//		}
//	}
	static class cmp implements Comparator<hh>{
		public int compare(hh a,hh b) {
			if(a.x!=b.x) return b.x-a.x;//按x升序
			else return a.y-b.y;//如果x相等,按y升序
		}
	}
    public static void main(String[] args){
    	int n=cin.nextInt();
    	hh a [] = new hh [55];
    	for (int i = 0; i < n;i++) {
//    		a[i]=new hh();//这样写不能写构造函数~~
//    		a[i].x=cin.nextInt();
//    		a[i].y=cin.nextInt();
    		a[i]=new hh (cin.nextInt(),cin.nextInt());//这样需要写构造函数
    	}
    	Arrays.sort(a, 0,n,new cmp());//这样写排序需要new
    	for (int i = 0; i < n;i++) {
    		System.out.println(a[i].x+" "+a[i].y);
    	}
    }
}
/*输入样例
 5
 1 2
 2 3
 1 4
 2 5
 5 5
 */
/*输出样例 按x升序,x相等按y升序
1 2
1 4
2 3
2 5
5 5
*/
/*输出样例 按x升序,x相等按y降序
1 4
1 2
2 5
2 3
5 5
*/
/*输出样例 按x降序,x相等按y降序
5 5
2 5
2 3
1 4
1 2
*/
/*输出样例 按x降序,x相等按y升序
5 5
2 3
2 5
1 2
1 4
*/
 

two:

import java.util.*;
public class Main {
	static Scanner cin = new Scanner(System.in);
	static class hh {
		int x,y;
		public hh(int x,int y) {
			this.x=x;
			this.y=y;
		}
	}
	static Comparator<hh> cmp =new Comparator<hh>(){
		public int compare(hh a,hh b) {
			if(a.x!=b.x) return a.x-b.x;//按x升序
			else return a.y-b.y;//如果x相等,按y升序
		}
	};
//	static Comparator<hh> cmp =new Comparator<hh>(){
//		public int compare(hh a,hh b) {
//			if(a.x!=b.x) return a.x-b.x;//按x升序
//			else return b.y-a.y;//如果x相等,按y降序
//		}
//	};
//	static Comparator<hh> cmp =new Comparator<hh>(){
//		public int compare(hh a,hh b) {
//			if(a.x!=b.x) return b.x-a.x;//按x降序
//			else return b.y-a.y;//如果x相等,按y降序
//		}
//	};
//	static Comparator<hh> cmp =new Comparator<hh>(){
//		public int compare(hh a,hh b) {
//			if(a.x!=b.x) return b.x-a.x;//按x升序
//			else return a.y-b.y;//如果x相等,按y升序
//		}
//	};
	//注意有分号~~
    public static void main(String[] args){
    	int n=cin.nextInt();
    	hh a [] = new hh [55];
    	for (int i = 0; i < n;i++) {
//    		a[i]=new hh();//这样写不能写构造函数~~
//    		a[i].x=cin.nextInt();
//    		a[i].y=cin.nextInt();
    		a[i]=new hh (cin.nextInt(),cin.nextInt());//这样需要写构造函数
    	}
    	Arrays.sort(a, 0,n,cmp);//这样写排序不需要需要new
    	for (int i = 0; i < n;i++) {
    		System.out.println(a[i].x+" "+a[i].y);
    	}
    }
}
/*输入样例
 5
 1 2
 2 3
 1 4
 2 5
 5 5
 */
/*输出样例 按x升序,x相等按y升序
1 2
1 4
2 3
2 5
5 5
*/
/*输出样例 按x升序,x相等按y降序
1 4
1 2
2 5
2 3
5 5
*/
/*输出样例 按x降序,x相等按y降序
5 5
2 5
2 3
1 4
1 2
*/
/*输出样例 按x降序,x相等按y升序
5 5
2 3
2 5
1 2
1 4
*/
 

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

心脏dance

如果解决了您的疑惑,谢谢打赏呦

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值