Java之类的排序(Sort)

Java语言内置的Arrays.sort可以帮我们使用快速排序完成序列的排序操作,下面是对sort使用的一个总结:

1.对基本数据类型数组进行排序

首先,基本数据类型包括:byte/short/int/long/char/double/float这些,没有。对这些数组的排序可以直接调用Arrays.sort([数组名]):
(直接调用是按照值的大小升序排序,即由小到大排序)

import java.util.Arrays;

public class Main {
	public static void main(String[] args) {
		int[] arr = { 1, 3, 2, 44, 22, 555, -2 };
		Arrays.sort(arr);
		for (int x : arr) {
			System.out.println(x);
		}
	}

}

结果如下:

-2
1
2
3
22
44
555

若需要倒排则需要改用包装类并重新定义比较方法,具体见Java用sort实现对数组的降序排序

2.对复杂对象数组进行排序

对象数组对排序要保证每一个元素都已经初始化了,否则会出现空指针异常,然后还需要定义两个对象比较的方法:

import java.util.Arrays;
import java.util.Comparator;

class Student {
	private double math;
	private double english;

	public double getMath() {
		return math;
	}

	public void setMath(double math) {
		this.math = math;
	}

	public double getEnglish() {
		return english;
	}

	public void setEnglish(double english) {
		this.english = english;
	}

}

public class Main {
	public static void main(String[] args) {

		// step1 声明数组
		// 定义一个Student数组,长度为5
		// 【注】Java中对象数组的声明不会创建对象,数组中的每一个对象元素只是初始化为null
		// 不可以直接使用 会导致NullPointerException异常 使用之前需要使用new实例化
		Student[] students = new Student[3];
		students[0] = new Student();
		students[0].setEnglish(88.8);
		students[0].setMath(99);
		students[1] = new Student();
		students[1].setEnglish(98.8);
		students[1].setMath(91);
		students[2] = new Student();
		students[2].setEnglish(82.3);
		students[2].setMath(83.6);

		// step2 使用sort排序
		// 使用sort的简单方式
		Arrays.sort(students, new Comparator<Student>() {

			// 定义比较两个对象的方法
			// 规则:o1在o2之前 则返回小于零的数,比如-1
			// o1等于o2 则返回零(0)
			// o1应在o2之后 则返回大于零的数,比如1

			// 例如按照英语成绩降序
			@Override
			public int compare(Student o1, Student o2) {
				if (o1.getEnglish() > o2.getEnglish())
					return -1;
				else if (o1.getEnglish() == o2.getEnglish()) {
					return 0;
				}
				return 1;
			}

		});

		// 看下结果
		for (int i = 0; i < students.length; i++) {
			System.out.println("eng : " + students[i].getEnglish() + " math :" + students[i].getMath());
		}

	}

}

结果:

eng : 98.8 math :91.0
eng : 88.8 math :99.0
eng : 82.3 math :83.6

其实对于基本的数据类型,其包装类都含有对应的比较方法,且都是按照从小到大的方式比较,所以上述的比较方法可以改成这样:

	// 例如按照英语成绩降序
	@Override
	public int compare(Student o1, Student o2) {
		//默认的compare按照升序,这里要求降序排,所以可以乘-1
		return Double.compare(o1.getEnglish(), o2.getEnglish()) * -1;
	}

【附】eclipse中如何快速写好以上的比较部分的代码?
1.在需要使用sort的地方写下如下代码:
其中第一个参数是待排序的对象数组变量名

Arrays.sort(students , new comp);

在这里插入图片描述
2.把鼠标光标放在comp之后
在这里插入图片描述
按下Alt + ?,弹出提示选择第一个:
在这里插入图片描述
然后就是这样:
在这里插入图片描述
3.把T换成咱们这次排序的对象数组的对象类:
在这里插入图片描述
4.把鼠标悬浮放在错误横线上,等待1秒左右,出现以下内容,点击Add unimplemented methods

在这里插入图片描述
5. 按照规则写好比较的方法即可:
在这里插入图片描述

3.对复杂自定义对象的另一种排序的方式----implement the Comparable interface(实现Comparable 接口)

Arrays.sort([]Object obj)

Api原文如下:

Sorts the specified array of objects into ascending order, according to the natural ordering of its elements. All elements in the array must implement the Comparable interface. Furthermore, all elements in the array must be mutually comparable (that is, e1.compareTo(e2) must not throw a ClassCastException for any elements e1 and e2 in the array).

大体意思是将对象数组按照升序排序,要保证所有的对象可以使用.compareTo()方法进行比较,也就是对象所属的类实现了Comparable接口,下面就是对这种方式使用sort的一个实例:

import java.util.Arrays;

class Student implements Comparable<Student> {
	private double math;
	private double english;

	public double getMath() {
		return math;
	}

	public void setMath(double math) {
		this.math = math;
	}

	public double getEnglish() {
		return english;
	}

	public void setEnglish(double english) {
		this.english = english;
	}

	@Override
	public int compareTo(Student student) {
		return Double.compare(this.english, student.english) * -1;
	}

}

public class Main {

	public static void main(String[] args) {
		Student[] students = new Student[3];
		students[0] = new Student();
		students[0].setEnglish(88.8);
		students[0].setMath(99);
		students[1] = new Student();
		students[1].setEnglish(98.8);
		students[1].setMath(91);
		students[2] = new Student();
		students[2].setEnglish(82.3);
		students[2].setMath(83.6);

		// 直接调用sort即可
		Arrays.sort(students);
		// 看下结果
		for (int i = 0; i < students.length; i++) {
			System.out.println("eng : " + students[i].getEnglish() + " math :" + students[i].getMath());
		}
	}
}

运行结果:

eng : 98.8 math :91.0
eng : 88.8 math :99.0
eng : 82.3 math :83.6

可以看出,结果一致,对比两种方法:
1.工程中,确定了两个对象的比较策略了,且不会变化的,可以使用后一种方法。
2.若对象排序的策略总会变化,且不容易确定,或在算法竞赛中为了提高编码的效率时则可以使用第一种方法。

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值