码迷,mamicode.com
首页 > 编程语言 > 详细

自定义多列排序:C++/Java实现

时间:2015-08-16 15:02:38      阅读:145      评论:0      收藏:0      [点我收藏+]

标签:

前言:

  有些时候,我们在编程中会遇到多列排序的需求。假如在execle,这事儿就太easy了。不过没办法,现在就需要你用Java或者C++实现这样一个功能!

比如将下表无序的数据通过重排之后按照以下规则显示结果:

1.第二列从大到小排列

2.若第二列相等,则第一列按照从大到小排序

 

排序前

排序后

2 5
3 2
2 1
11 75
21 101
32 21
20 59
13 21

21 101
11 75
20 59
13 21
32 21
2 5
3 2
2 1

 

 

 

 

 

 

 

 

-------------------------------------

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 }

 

自定义多列排序:C++/Java实现

标签:

原文地址:http://www.cnblogs.com/SeaSky0606/p/4734239.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!