Java自定义 sort 排序方法
Sort用法
•结构
1 package Test; 2 3 import java.util.Arrays; 4 import java.util.Random; 5 import java.util.Scanner; 6 7 public class TestSort { 8 9 static int a[] = new int[10]; 10 static Random random = new Random(); 11 static Scanner cin = new Scanner(System.in); 12 13 public static void main(String[] args) { 14 15 } 16 17 public static void Print(int[] a,int len) { 18 for(int i = 0;i < len;i++) 19 System.out.print(a[i]+" "); 20 System.out.println(); 21 } 22 }
•Arrays.sort(a)
1 public static void main(String[] args) { 2 3 for(int i = 0;i <= 5;i++) { 4 a[i] = random.nextInt(10); 5 } 6 7 Print(a,a.length);///只对 a[0,1,2,...,5] 区间进行了赋值操作,a[6,7,8,...,a.length-1]全为 0 8 Arrays.sort(a);///对 a 中 [0,a.length) 区间进行升序排列 9 Print(a,a.length); 10 }•运行结果
•Arrays.sort(a,x,y)
1 public static void main(String[] args) { 2 3 for(int i = 0;i <= 5;i++) { 4 a[i] = random.nextInt(10)+1; 5 } 6 7 Print(a,a.length);///只对 a[0,1,2,...,5] 区间进行了赋值操作,a[6,7,8,...,a.length-1]全为 0 8 Arrays.sort(a,0,6);///对 a 中 [0,6) 区间进行升序排列 9 Print(a,a.length); 10 }•运行结果
自定义Sort排序
•对数组自定义排序
1 ///对 num [1,n+1)区间进行自定义排序 2 Arrays.sort(num,1,n+1,new Comparator<Integer>() { 3 public int compare(Integer o1,Integer o2) { 4 return o1-o2;///从小到大排序 5 //return o2-o1;///从大到小排序 6 } 7 });PS : Arrays.sort所排序的是对象类型,如果对int类型的数组进行sort排序,那就需要通过包装类 Integer 来实现。
所以 num 是 Integer 类型的数组。
•牛刀小试HDU2020
链接:绝对值排序
•Code
View Code1 import java.util.Arrays; 2 import java.util.Comparator; 3 import java.util.Random; 4 import java.util.Scanner; 5 6 public class Main{ 7 8 static int n; 9 static Integer num[] = new Integer[150]; 10 public static void main(String[] args) { 11 12 Scanner cin = new Scanner(System.in); 13 14 while(cin.hasNext()) { 15 n = cin.nextInt(); 16 if(n == 0) 17 break; 18 19 for(int i = 1;i <= n;i++) { 20 num[i] = cin.nextInt(); 21 } 22 23 Arrays.sort(num,1,n+1,new Comparator<Integer>(){ 24 @Override 25 public int compare(Integer o1, Integer o2) { 26 // TODO Auto-generated method stub 27 return Math.abs(o2)-Math.abs(o1); 28 } 29 30 }); 31 for(int i = 1;i <= n;i++) { 32 if(i != 1) 33 System.out.print(" "); 34 System.out.print(num[i]); 35 } 36 System.out.println(); 37 } 38 } 39 40 }
•对Class自定义排序
1 class F{ 2 int x,y; 3 } 4 class cmp implements Comparator<F>{ 5 6 @Override 7 public int compare(F o1, F o2) { 8 // TODO Auto-generated method stub 9 if(o1.x != o2.x) {//先按 x 从小到达排序 10 return o1.x > o2.x ? 1:-1; 11 } 12 else//x 相同,按照 y 从小到大排序 13 return o1.y > o2.y ? 1:-1; 14 } 15 }•Code
View Code1 package Test; 2 3 import java.util.Arrays; 4 import java.util.Comparator; 5 import java.util.Random; 6 import java.util.Scanner; 7 8 public class TestSortClass { 9 10 static F[] f = new F[5]; 11 static Random random = new Random(); 12 static Scanner cin = new Scanner(System.in); 13 14 public static void main(String[] args) { 15 16 for(int i = 0;i < 5;i++) { 17 f[i] = new F(); 18 f[i].x = random.nextInt(5); 19 f[i].y = random.nextInt(10); 20 } 21 22 Print(f,f.length); 23 Arrays.sort(f,0,5,new cmp()); 24 Print(f,f.length); 25 } 26 27 private static void Print(F[] f,int len) { 28 // TODO Auto-generated method stub 29 System.out.println("**********"); 30 for(int i = 0;i < len;i++) { 31 System.out.println(f[i].x + " " + f[i].y); 32 } 33 } 34 } 35 36 class F{ 37 int x,y; 38 } 39 class cmp implements Comparator<F>{ 40 41 @Override 42 public int compare(F o1, F o2) { 43 // TODO Auto-generated method stub 44 if(o1.x != o2.x) {//先按 x 从小到达排序 45 return o1.x > o2.x ? 1:-1; 46 } 47 else//x 相同,按照 y 从小到大排序 48 return o1.y > o2.y ? 1:-1; 49 } 50 }运行结果:
•牛刀小试HDU2022
链接:海选女主角
•Code
View Code1 import java.util.Arrays; 2 import java.util.Comparator; 3 import java.util.Scanner; 4 5 public class HDU2022 { 6 7 static int m,n; 8 static F[] f = new F[10000]; 9 public static void main(String[] args){ 10 Scanner cin = new Scanner(System.in); 11 12 while(cin.hasNext()) { 13 m = cin.nextInt(); 14 n = cin.nextInt(); 15 16 int index = 0; 17 for(int i = 1;i <= m;i++) { 18 for(int j = 1;j <= n;j++) { 19 f[index] = new F(); 20 f[index].point = cin.nextLong(); 21 f[index].x = i; 22 f[index].y = j; 23 index++; 24 } 25 } 26 Arrays.sort(f,0,index,new cmp()); 27 28 System.out.println(f[0].x+" "+f[0].y+" "+f[0].point); 29 } 30 } 31 } 32 class F{ 33 long point; 34 int x,y; 35 } 36 class cmp implements Comparator<F>{ 37 38 @Override 39 public int compare(F o1, F o2) { 40 if(o1.point != o2.point)//按照得分的绝对值从大到小排序 41 return Math.abs(o2.point) > Math.abs(o1.point) ? 1:-1; 42 else if(o1.x != o2.x) {//按照行从小到大排序 43 return (o1.x > o2.x) ? 1:-1; 44 } 45 else {//按照列从小到大排序 46 return (o1.y > o2.y) ? 1:-1; 47 } 48 } 49 }