Java中sort如何自定义排序
2021-06-10 15:05
标签:php tin sort close 默认 自定义 imp ati targe PS : num 必须是 Integer类型的 链接:绝对值排序 Java中sort如何自定义排序 标签:php tin sort close 默认 自定义 imp ati targe 原文地址:https://www.cnblogs.com/hyacinthLJP/p/14250130.html
?自定义排序格式
///对 num [1,n+1)区间进行自定义排序
Arrays.sort(num,1,n+1,new Comparator
1 Arrays.sort(num,1,n+1,new Comparator
?Code
1 package Test;
2
3 import java.util.Arrays;
4 import java.util.Comparator;
5 import java.util.Random;
6
7 public class Sort {
8
9 static Random random = new Random();
10 public static void main(String[] args) {
11
12
13 Integer[] num = new Integer[10];
14
15 int n = random.nextInt(5)+5;
16
17 System.out.println("测试默认排序方式");
18 R(num,n);///随机 n 个数
19 Print(num,n);
20 Arrays.sort(num,1,n+1);///[1,n+1)从小到大排序
21 Print(num,n);
22 System.out.println("+-+-+-+-+-+-+-+-+"+"\n");
23
24 /*
25 * o1-o2 : 从小到大排序
26 * o2-o1 : 从大到小排序
27 * Math.abs(o2)-Math.abs(o1) : 绝对值从大到小排序
28 */
29 System.out.println("测试自定义排序方式");
30 R(num,n);
31 Print(num,n);
32 Arrays.sort(num,1,n+1,new Comparator
?牛刀小试HDU2020
?Code
1 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 ) {
20 num[i] = cin.nextInt();
21 }
22
23 Arrays.sort(num,1,n+1,new Comparator