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. flex item default All In One

    flex item default All In One flex item default 初始值 === flex: 0 1 auto; https://drafts.csswg.org/css- ...

  2. React + GraphQL 2020 速成课程

    React + GraphQL 2020 速成课程 technologies React (to build our user interface) GraphQL (to get and chang ...

  3. LGTM & code review

    LGTM & code review LGTM is an acronym meaning looks good to me, frequently used when reviewing d ...

  4. taro router

    taro router https://nervjs.github.io/taro/docs/router.html bug import Taro, { Component, Config } fr ...

  5. zrender & svg

    zrender & svg window.prompt double click https://codepen.io/xgqfrms/pen/jOEGNvw // https://cdn.x ...

  6. 文件描述符(File Descriptor)简介

    本文转载自文件描述符(File Descriptor)简介 导语 维基百科:文件描述符在形式上是一个非负整数.实际上,它是一个索引值,指向内核为每一个进程所维护的该进程打开文件的记录表.当程序打开一个 ...

  7. 翻译:《实用的Python编程》02_06_List_comprehension

    目录 | 上一节 (2.5 collections模块) | 下一节 (2.7 对象模型) 2.6 列表推导式 一个常见的任务是处理列表中的项(译注:元素).本节介绍列表推导式,完成此任务的强大工具. ...

  8. 阿里云CentOS8.0服务器配置Django3.0+Python 3.7 环境

    ---恢复内容开始--- 1. 下载并安装python # 安装Python3.7.6 wget https://www.python.org/ftp/python/3.7.6/Python-3.7. ...

  9. es6 快速入门 系列 —— 变量声明:let和const

    其他章节请看: es6 快速入门 系列 变量声明:let和const 试图解决的问题 经典的 var 声明让人迷惑 function demo1(v){ if(v){ var color='red' ...

  10. C#关于个Base64,MD5,16进制的转换

    1,待签名数据以UTF-8的格式转字节流,对字节流进行MD5算法得到的签名字节流,再转换为16进制字符串,即生成了数字签名. byte[] targetData = md5.ComputeHash(S ...