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

 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 <= 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

 1 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

 1 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 }

Java自定义 sort 排序方法的更多相关文章

  1. JAVA Collections工具类sort()排序方法

    主要分析内容: 一.Collections工具类两种sort()方法 二.示例 一.Collections工具类两种sort()方法 格式一: public static <T extends ...

  2. 自定义sort排序

    java的sort自定义: 1.排序对象必须是封装类而不能是基本数据类型: 2.调用Arrays.sort(array, left, right, cmp)进行排序,array为数组,left.rig ...

  3. 关于Collections.sort()排序方法的源码探索

    /**下面在自己代码中使用Collections.sort()方法去比较Student对象,通过在自己写的类里面通过匿名内部类实现Comparator接口,这个接口是让你自己实现比较器的规则*/ // ...

  4. java自定义注解注解方法、类、属性等等【转】

    http://anole1982.iteye.com/blog/1450421 http://www.open-open.com/doc/view/51fe76de67214563b20b385320 ...

  5. js数组sort排序方法的算法

    说明一下,ECMAScript没有定义使用哪种排序算法,各个浏览器的实现方式会有不同.火狐中使用的是归并排序,下面是Chrome的sort排序算法的实现. sort方法源码 DEFINE_METHOD ...

  6. 解析JavaScript中的sort()排序方法以及原理

    Array.sort()方法将数组中的元素进行排序,返回排序后的数组,默认是按照升序排序的.sort方法会调用数组中每一项的toString()方法,然后按照ascii编码进行排序,如果数组含有und ...

  7. DataTable.DefaultView.Sort 排序方法

    今天在整合一个东西,需要用到DataTable的一个排序方法, 前我是将DataTable存到DataView里面的,所以刚开始就使用了DataView.Sort="ColumnName A ...

  8. MongoDB limit 选取 skip跳过 sort排序 方法

    MongoDB  limit 选取 skip跳过 sort排序 在mysql里有order by  MongoDB用sort代替order by > db.user.find() { " ...

  9. 个人对sort()排序方法中比较函数一直很混乱,今日理清

    需求:使用随机数来打印出0-10,并排序. 代码: var a = new Array();var testArray = function() { while (1) { var b = parse ...

随机推荐

  1. ES6 Map vs ES5 Object

    ES6 Map vs ES5 Object Map vs Object https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc ...

  2. 从长度为 M 的无序数组中,找出N个最小的数

    从长度为 M 的无序数组中,找出 N个最小的数 在一组长度为 n 的无序的数组中,取最小的 m个数(m < n), 要求时间复杂度 O(m * n) 网易有道面试题 const minTopK ...

  3. H5 APP 页面移动端适配方案

    H5 APP 页面移动端适配方案 https://segmentfault.com/a/1190000011586301 https://juejin.im/post/5cbdee71f265da03 ...

  4. css dark theme & js theme checker

    css dark theme & js theme checker live demo https://codepen.io/xgqfrms/pen/GRprYLm <!DOCTYPE ...

  5. taro demos & taro 组件库

    taro demos & taro 组件库 ui demo https://github.com/qit-team/taro-yanxuan https://github.com/fengch ...

  6. django学习-21.优化表数据的标题展示

    目录结构 1.前言 2.表数据的标题默认展示的数据格式是[模型类名 object(主键名)]的相关信息 3.优化表数据的标题展示的数据格式是[改成我们想要展示的数据格式]的相关完整操作步骤 3.1.第 ...

  7. 教你吃透CSS的盒子模型(Box Model)

    CSS 盒子模型(Box Model) 所有HTML元素可以看作盒子,在CSS中,"box model"这一术语是用来设计和布局时使用. CSS盒模型本质上是一个盒子,封装周围的H ...

  8. SSL (Secure Sockets Layer)

    本文转载自SSL (Secure Sockets Layer) TLS简介 The Transport Layer Security (TLS) protocol aims primarily to ...

  9. MySQL应用优化

    MySQL应用优化 目录 MySQL应用优化 1.数据库连接池 2.减少对MySQL的访问 3.负载均衡 4.MySQL查询缓存优化 5.MySQL如何使用缓存 6.MySQL内存管理以及优化 原则 ...

  10. 微信小程序:block标签

    代码中存在block标签,但是渲染的时候会移除掉. 例子: 如果将view改为block: 当你要渲染某些数据时,如果不想额外的加一层外边的标签,此时可以使用block标签来进行占位.