自定义多列排序:C++/Java实现
生活随笔
收集整理的這篇文章主要介紹了
自定义多列排序:C++/Java实现
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
前言:
有些時候,我們在編程中會遇到多列排序的需求。假如在execle,這事兒就太easy了。不過沒辦法,現在就需要你用Java或者C++實現這樣一個功能!
比如將下表無序的數據通過重排之后按照以下規則顯示結果:
1.第二列從大到小排列
2.若第二列相等,則第一列按照從小到大排序
?
| 排序前 | 排序后 |
| 2 5 | 21 101 |
?
?
?
?
?
?
?
?
-------------------------------------
C++實現一:運算符重載
1 #include <cstdio> 2 #include <vector> 3 #include <algorithm> 4 using namespace std; 5 6 const int maxn = 1e4+5; 7 struct Job{ 8 int x,y; 9 bool operator < (const Job &j) const { //operator 10 if(y==j.y){ 11 return x < j.x; 12 } 13 return y > j.y; 14 } 15 }; 16 17 int main(){ 18 // 從文件讀入 19 freopen("datain.txt","r",stdin); 20 freopen("dataout.txt","w",stdout); 21 int n,x,y; 22 while(scanf("%d",&n)==1 && n){ 23 vector<Job> v; 24 for(int i=0;i<n;i++){ 25 scanf("%d%d",&x,&y); 26 v.push_back(Job{x,y}); 27 } 28 29 sort(v.begin(),v.end()); 30 //輸出 31 for(int i=0;i<n;i++){ 32 printf("%d %d\n",v[i].x,v[i].y); 33 } 34 35 } 36 return 0; 37 }?
C++實現二:重寫cmp()比較函數
1 #include <cstdio> 2 #include <algorithm> 3 using namespace std; 4 5 const int maxn = 1e4+5; 6 struct Job{ 7 int x,y; 8 9 }; 10 bool cmp(Job j1,Job j2){ 11 if(j1.y==j2.y) 12 return j1.x < j2.x; 13 return j1.y > j2.y; 14 } 15 16 Job A[maxn]; 17 int main(){ 18 //將數據從文件讀入 19 freopen("datain.txt","r",stdin); 20 int n; 21 while(scanf("%d",&n)==1 && n){ 22 for(int i=0;i<n;i++){ 23 scanf("%d%d",&A[i].x,&A[i].y); 24 } 25 26 sort(A,A+n,cmp); 27 for(int i=0;i<n;i++){ 28 printf("%d %d\n",A[i].x,A[i].y); 29 } 30 31 } 32 return 0; 33 }?
?
Java實現一:實現Comparable接口,重寫compareTo()方法
1 package sort; 2 3 import java.util.ArrayList; 4 import java.util.Collections; 5 import java.util.List; 6 7 /* 8 * 重寫Comparable接口的campareTo(Object o)方法 9 */ 10 class Task implements Comparable<Task>{ 11 int x,y; 12 13 public Task(int x,int y) { 14 this.x=x; 15 this.y=y; 16 } 17 @Override 18 public int compareTo(Task o) { 19 if(this.y==o.y) 20 return this.x-o.x; 21 return o.y-this.y; 22 } 23 @Override 24 public String toString() { 25 String r = this.x+" "+this.y; 26 return r; 27 } 28 29 } 30 public class TestCompare { 31 public static void main(String[] args) { 32 Task t1 = new Task(5,2); 33 Task t2 = new Task(5,4); 34 Task t3 = new Task(3,2); 35 36 List<Task> tasks = new ArrayList<Task>(); 37 tasks.add(t1); 38 tasks.add(t2); 39 tasks.add(t3); 40 41 //排序sort 42 Collections.sort(tasks); 43 44 //打印輸出 45 for(Task t:tasks){ 46 System.out.println(t); 47 } 48 } 49 }?
Java實現二:重寫compare方法
1 package sort; 2 3 import java.util.ArrayList; 4 import java.util.Comparator; 5 import java.util.List; 6 7 import com.gdufe.mian4.Collection; 8 import com.sun.xml.internal.ws.policy.privateutil.PolicyUtils.Collections; 9 10 class Job { 11 int x; 12 int y; 13 14 public Job(int x, int y) { 15 this.x = x; 16 this.y = y; 17 } 18 19 public String toString() { 20 String r = this.x + " " + this.y; 21 return r; 22 } 23 } 24 25 public class TestCompare2 { 26 27 public static void main(String[] args) { 28 List<Job> jobs = new ArrayList<Job>(); 29 Job j1 = new Job(2,4); 30 Job j2 = new Job(5,3); 31 Job j3 = new Job(1,4); 32 jobs.add(j1); 33 jobs.add(j2); 34 jobs.add(j3); 35 36 37 //重寫Comparator接口的Compare方法:先按照第二列從大到小,若第二列相等則第一列從小到大排序 38 Comparator<Job> c = new Comparator<Job>() { 39 40 @Override 41 public int compare(Job o1, Job o2) { 42 if(o1.y==o2.y){ 43 return o1.x-o2.x; 44 } 45 return o2.y-o1.y; 46 } 47 }; 48 49 System.out.println("輸出排序后的結果:"); 50 java.util.Collections.sort(jobs, c); 51 for(Job job:jobs){ 52 System.out.println(job); 53 } 54 55 } 56 57 }?
轉載于:https://www.cnblogs.com/SeaSky0606/p/4734239.html
總結
以上是生活随笔為你收集整理的自定义多列排序:C++/Java实现的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 唐诗三百首加密软件如何使用_绿盾加密软件
- 下一篇: 全国计算机等级考试题库二级C操作题100