Java中的数组排序,一般是利用Arrays.sort(),这个方法是经过优化的快速排序。在Arrays种有多中形式的重载,在这里就不一一列举了。

数组排序的种类:

1.非降序排序, 非升序排序(就排序后数组元素排列的结果而言)

2.基本类型数据的排序,类类型数据的排序(就排序的对象而言)

排序示例:

int型数组的非降序排序:

  1. package sort;
  2.  
  3. import java.util.Arrays;
  4.  
  5. public class Main {
  6. public static void displayArray(int[] array) {
  7. for (int i: array) {
  8. System.out.print(i + " ");
  9. }
  10. System.out.println();
  11. }
  12.  
  13. public static void main(String[] args) {
  14. int[] arr = new int[]{43, 84, 3, 8, 4, 7, 3, 75, 82, 748, 35};
  15.  
  16. System.out.println("排序前:");
  17. displayArray(arr);
  18.  
  19. Arrays.sort(arr);
  20.  
  21. System.out.println("排序后:");
  22. displayArray(arr);
  23. }
  24. }

运行结果如下:

int型数组的非升序排序:

  1. package sort;
  2.  
  3. import java.util.Arrays;
  4. import java.util.Comparator;
  5.  
  6. public class Main {
  7. public static void displayArray(Integer[] array) {
  8. for (int i: array) {
  9. System.out.print(i + " ");
  10. }
  11. System.out.println();
  12. }
  13.  
  14. public static void main(String[] args) {
  15. Integer[] arr = new Integer[]{43, 84, 3, 8, 4, 7, 3, 75, 82, 748, 35};
  16.  
  17. System.out.println("排序前:");
  18. displayArray(arr);
  19.  
  20. Arrays.sort(arr, new JX());
  21.  
  22. System.out.println("排序后:");
  23. displayArray(arr);
  24. }
  25. }
  26.  
  27. class JX implements Comparator<Integer> {
  28. @Override
  29. public int compare(Integer o1, Integer o2) {
  30. if (o1 <= o2) {
  31. return 1;
  32. }
  33. return -1;
  34. }
  35. }

运行结果如下:

类类型的非降序排序:

  1. package sort;
  2.  
  3. import java.util.Arrays;
  4.  
  5. public class Main {
  6. public static void displayArray(Student[] student) {
  7. for (Student s : student) {
  8. System.out.println(s);
  9. }
  10. }
  11.  
  12. public static void main(String[] args) {
  13. Student[] s = new Student[5];
  14. s[0] = new Student("wwww", 20, 2.34);
  15. s[1] = new Student("kkkk", 2, 2.34);
  16. s[2] = new Student("pppp", 25, 3.34);
  17. s[3] = new Student("hhhh", 12, 4.34);
  18. s[4] = new Student("jjjj", 10, 5.34);
  19.  
  20. System.out.println("排序前:");
  21. displayArray(s);
  22.  
  23. Arrays.sort(s);
  24.  
  25. System.out.println("排序后:");
  26. displayArray(s);
  27. }
  28. }
  29.  
  30. /*
  31. * 根据年龄进行非降序排序
  32. */
  33. class Student implements Comparable<Student> {
  34. private String name;
  35. private int age;
  36. private double height;
  37.  
  38. public Student(String name, int age, double height) {
  39. this.name = name;
  40. this.age = age;
  41. this.height = height;
  42. }
  43.  
  44. @Override
  45. public int compareTo(Student o) {
  46. if (this.age <= o.age) {
  47. return -1;
  48. }
  49. return 1;
  50. }
  51.  
  52. public String toString() {
  53. return "Name: " + name + " Age: " + age + " Height: " + height;
  54. }
  55. }

运行结果如下:

类类型的非升序排序:

  1. package sort;
  2.  
  3. import java.util.Arrays;
  4.  
  5. public class Main {
  6. public static void displayArray(Student[] student) {
  7. for (Student s : student) {
  8. System.out.println(s);
  9. }
  10. }
  11.  
  12. public static void main(String[] args) {
  13. Student[] s = new Student[5];
  14. s[0] = new Student("wwww", 20, 2.34);
  15. s[1] = new Student("kkkk", 2, 2.34);
  16. s[2] = new Student("pppp", 25, 3.34);
  17. s[3] = new Student("hhhh", 12, 4.34);
  18. s[4] = new Student("jjjj", 10, 5.34);
  19.  
  20. System.out.println("排序前:");
  21. displayArray(s);
  22.  
  23. Arrays.sort(s);
  24.  
  25. System.out.println("排序后:");
  26. displayArray(s);
  27. }
  28. }
  29.  
  30. /*
  31. * 根据年龄进行非升序排序
  32. */
  33. class Student implements Comparable<Student> {
  34. private String name;
  35. private int age;
  36. private double height;
  37.  
  38. public Student(String name, int age, double height) {
  39. this.name = name;
  40. this.age = age;
  41. this.height = height;
  42. }
  43.  
  44. @Override
  45. public int compareTo(Student o) {
  46. if (this.age <= o.age) {
  47. return 1;
  48. }
  49. return -1;
  50. }
  51.  
  52. public String toString() {
  53. return "Name: " + name + " Age: " + age + " Height: " + height;
  54. }
  55. }

运行结果如下:

根据指定属性对类类型数组排序:

  1. package sort;
  2.  
  3. import java.util.Arrays;
  4. import java.util.Comparator;
  5.  
  6. public class Main {
  7. public static void displayArray(Student[] student) {
  8. for (Student s : student) {
  9. System.out.println(s);
  10. }
  11. }
  12.  
  13. public static void main(String[] args) {
  14. Student[] s = new Student[5];
  15. s[0] = new Student("wwww", 20, 2.34);
  16. s[1] = new Student("kkkk", 2, 2.34);
  17. s[2] = new Student("pppp", 25, 3.34);
  18. s[3] = new Student("hhhh", 12, 4.34);
  19. s[4] = new Student("jjjj", 10, 5.34);
  20. /*
  21. System.out.println("排序前:");
  22. displayArray(s);
  23. */
  24. System.out.println("按age进行非降序排序");
  25. Arrays.sort(s, new SortByAge());
  26. displayArray(s);
  27.  
  28. System.out.println("按height进行非升序排序");
  29. Arrays.sort(s, new SortByHeight());
  30. displayArray(s);
  31. }
  32. }
  33.  
  34. class Student {
  35. private String name;
  36. private int age;
  37. private double height;
  38.  
  39. public Student(String name, int age, double height) {
  40. this.name = name;
  41. this.age = age;
  42. this.height = height;
  43. }
  44.  
  45. public String toString() {
  46. return "Name: " + name + " Age: " + age + " Height: " + height;
  47. }
  48.  
  49. public int getAge() {
  50. return age;
  51. }
  52.  
  53. public double getHeight() {
  54. return height;
  55. }
  56. }
  57.  
  58. /*
  59. * 按age进行非降序排序
  60. */
  61. class SortByAge implements Comparator<Student> {
  62. @Override
  63. public int compare(Student o1, Student o2) {
  64. if (o1.getAge() <= o2.getAge()) {
  65. return -1;
  66. }
  67. return 1;
  68. }
  69. }
  70.  
  71. /*
  72. * 按height进行非升序排序
  73. */
  74. class SortByHeight implements Comparator<Student> {
  75. @Override
  76. public int compare(Student o1, Student o2) {
  77. if (o1.getHeight() - o2.getHeight() < 0.01) {
  78. return 1;
  79. }
  80. return -1;
  81. }
  82.  
  83. }

运行结果如下:

Java中的数组排序的更多相关文章

  1. java中sort方法的自定义比较器写法(转载)

    java中sort方法的自定义比较器写法 摘要 在做一些算法题时常常会需要对数组.自定义对象.集合进行排序. 在java中对数组排序提供了Arrays.sort()方法,对集合排序提供Collecti ...

  2. java中Comparator的用法 -- 实现集合和数组排序

    在java中,如果要对集合对象或数组对象进行排序,需要实现Comparator接口以达到我们想要的目标. 接下来我们模拟下在集合对象中对日期属性进行排序 一.实体类Step package com.l ...

  3. Java 中的数组操作

    前言 在Java中,有很多封装好的类可以用来操纵数组(排序,复制等等),使得数组使用起来非常的方便.这就是高级语言带来的好处. 代码示例 - 一维数组 package test; import jav ...

  4. 数组在C++和java中的区别

    几乎所有的程序设计语言都支持数组.在C和C++中使用数组是很危险的.因为C和C++中的数组就是内存块.如果一个程序要访问其自身内存块之外的数组,或者在数组初始化之前使用它,都会产生难以预料的后果. j ...

  5. 【JAVA零基础入门系列】Day10 Java中的数组

    什么是数组?顾名思义,就是数据的组合,把一些相同类型的数放到一组里去. 那为什么要用数组呢?比如需要统计全班同学的成绩的时候,如果给班上50个同学的成绩信息都命名一个变量进行存储,显然不方便,而且在做 ...

  6. 【Java学习笔记之十】Java中循环语句foreach使用总结及foreach写法失效的问题

    foreach语句使用总结 增强for(part1:part2){part3}; part2中是一个数组对象,或者是带有泛性的集合. part1定义了一个局部变量,这个局部变量的类型与part2中的对 ...

  7. Java中常见的比较器的实现方法

    在Java中经常会涉及到对象数组的排序问题,那么就涉及到对象之间的比较问题. 通常对象之间的比较可以从两个方面去看: 第一个方面:对象的地址是否一样,也就是是否引用自同一个对象.这种方式可以直接使用& ...

  8. javascript中对两个对象进行排序 和 java中的两个对象排序

    javascript中的对象数组排序 一 定义一个对象数组 var text = [{"name":"张","age":24},{" ...

  9. 【Java】Java中的Collections类——Java中升级版的数据结构【转】

    一般来说课本上的数据结构包括数组.单链表.堆栈.树.图.我这里所指的数据结构,是一个怎么表示一个对象的问题,有时候,单单一个变量声明不堪大用,比如int,String,double甚至一维数组.二维数 ...

随机推荐

  1. POJ 2406 Power Strings KMP运用题解

    本题是计算一个字符串能完整分成多少一模一样的子字符串. 原来是使用KMP的next数组计算出来的,一直都认为是能够利用next数组的.可是自己想了非常久没能这么简洁地总结出来,也仅仅能查查他人代码才恍 ...

  2. 调用webservice查询手机号码归属地信息

    Web Services是由企业发布的完成其特定商务需求的在线应用服务,其他公司或应用软件能够通过Internet来访问并使用这项在线服务.在这里我们使用soap协议往webservice发送信息,然 ...

  3. LumiSoft.Net邮件接收乱码问题解决

    本文非本人编写,转载自:http://www.cnblogs.com/youngerliu/archive/2013/05/27/3101488.html 今天遇到用LumiSoft.Net这个组件收 ...

  4. JAVA反射机制学�

    JAVA反射机制:对于随意一个类,都可以知道这个类的全部属性和方法:对于随意一个对象,都可以调用它的随意一个方法和属性:这样的动态获取的信息以及动态调用对象的方法的功能称为java语言的反射机制. J ...

  5. iOS 2D绘图详解(Quartz 2D)之路径(点,直线,虚线,曲线,圆弧,椭圆,矩形)

    前言:一个路径可以包含由一个或者多个shape以及子路径subpath,quartz提供了很多方便的shape可以直接调用.例如:point,line,Arc(圆弧),Curves(曲线),Ellip ...

  6. apache常见错误汇总

    <>问题: Access forbidden! You don't have permission to access the requested directory. There is ...

  7. Spark_Api_图解

  8. MySQL查询缓存详解

    一:缓存条件,原理 MySQL Query Cache是用来缓存我们所执行的SELECT语句以及该语句的结果集,MySql在实现Query Cache的具体技术细节上类似典型的KV存储,就是将SELE ...

  9. javascript 关于语义化作用的理解

    看代码实例1 var a=1; function m(a){ //此处为形参第一个传入函数的参数,既为arguments[0] alert(a); //此处a为与形参绑定的 } m(a);//1 此时 ...

  10. Android图片选择器--仿QQ

    当做一款APP,需要选择本地图片时,首先考虑的无疑是系统相册,但是Android手机五花八门,再者手机像素的提升,大图无法返回等异常因数,导致适配机型比较困难,微信.QQ都相继的在自己的APP里集成了 ...