东莞营销网站建设多少钱,wordpress开场动画,河南省工程建设信息网官网入口,网站建设 英语翻译前言#xff1a; 有些时候#xff0c;我们在编程中会遇到多列排序的需求。假如在execle,这事儿就太easy了。不过没办法#xff0c;现在就需要你用Java或者C实现这样一个功能#xff01; 比如将下表无序的数据通过重排之后按照以下规则显示结果#xff1a; 1.第二列从大到小…前言 有些时候我们在编程中会遇到多列排序的需求。假如在execle,这事儿就太easy了。不过没办法现在就需要你用Java或者C实现这样一个功能 比如将下表无序的数据通过重排之后按照以下规则显示结果 1.第二列从大到小排列 2.若第二列相等则第一列按照从小到大排序 排序前 排序后 2 53 22 111 7521 10132 2120 5913 21 21 10111 7520 5913 2132 212 53 22 1 ------------------------------------- C实现一运算符重载 1 #include cstdio2 #include vector3 #include algorithm4 using namespace std;5 6 const int maxn 1e45;7 struct Job{8 int x,y;9 bool operator (const Job j) const { //operator
10 if(yj.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 vectorJob v;
24 for(int i0;in;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 i0;in;i){
32 printf(%d %d\n,v[i].x,v[i].y);
33 }
34
35 }
36 return 0;
37 } C实现二重写cmp()比较函数 1 #include cstdio2 #include algorithm3 using namespace std;4 5 const int maxn 1e45;6 struct Job{7 int x,y;8 9 };
10 bool cmp(Job j1,Job j2){
11 if(j1.yj2.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 i0;in;i){
23 scanf(%d%d,A[i].x,A[i].y);
24 }
25
26 sort(A,An,cmp);
27 for(int i0;in;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 ComparableTask{
11 int x,y;
12
13 public Task(int x,int y) {
14 this.xx;
15 this.yy;
16 }
17 Override
18 public int compareTo(Task o) {
19 if(this.yo.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 ListTask tasks new ArrayListTask();
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 ListJob jobs new ArrayListJob();
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 ComparatorJob c new ComparatorJob() {
39
40 Override
41 public int compare(Job o1, Job o2) {
42 if(o1.yo2.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