Java中的简单排序问题

Java中的排序问题

在Java中,排序非常简单,一行代码就可以了,这是因为我们有集合框架,ArrayList类中的sort排序方法让排序变得简单易懂

sort方法用法即用ArrayList对象调用sort(Comparator<? super E> c) 方法,括号里面的是Comparator接口的实现方法,简单来说排序的根据,比如按数字大小啊,英文字母的首字母排序啊等等,我们可以自己编写一个实现Comparator接口的类,其实就是实现Comparator类中的compare方法,然后sort方法括号中的即是这个类的对象;或者我们可以用λ表达式去写,代码量要少很多,lambda表达式很简单,()->{},()中是参数,{}中是方法;下面是我的排序实例:

先见一个Student类去作为排序的对象类(为了方便,这里只定义了三个属性):
/**
 * 学生类
 * (熟练运用Java中的快捷方式,创建set/get方法、构造器、toString方法、)
 * @author July.Jul
 *
 */
public class Student {

	/**学生姓名*/
	private String name;
	/**学生年龄*/
	private int age;
	/**学生成绩*/
	private double score;
	
	public Student() {
		// TODO Auto-generated constructor stub
	}

	public Student(String name, int age, double score) {
		this.name = name;
		this.age = age;
		this.score = score;
	}
	
	@Override
	public String toString() {
		return "Student [name=" + name + ", age=" + age + ", score=" + score + "]";
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public double getScore() {
		return score;
	}

	public void setScore(double score) {
		this.score = score;
	}
}
然后在Test类中创建一个ArrayList数组去装多个Student对象,利用sort方法对学生对象排序,主要就是对sort方法以及lambda表达式的一个简单应用,lambda表达式很简单,它只是一个为了简化代码的一个表达方式。(需要注意的是,直接用sort方法没办法比较中文汉字,所以我这里用了一个jar包去实现汉字转拼音比较!大家可以直接用英文名进行比较):
import java.util.ArrayList;

import net.sourceforge.pinyin4j.PinyinHelper;

public class Test2 {

	public static void main(String[] args) {
		
		ArrayList<Student> list = new ArrayList<>();
		list.add(new Student("穆桂英",21,65));
		list.add(new Student("张三丰",22,98));
		list.add(new Student("榴莲",18,75));
		list.add(new Student("芒果",20,74));
		list.add(new Student("加菲猫",19,83));
		list.add(new Student("大魔王",20,84));
		
		list.sort((a,b)->a.getAge()-b.getAge());
		for (Student student : list) {
			System.out.println(student);
		}
		System.out.println();
		
		list.sort((a,b)->Double.compare(a.getScore(),b.getScore()));
		for (Student student : list) {
			System.out.println(student);
		}
		System.out.println();
		
		/**无法比较汉字,需要汉字转拼音*/
		list.sort((a,b)->{
			String name1 = a.getName();
			String name2 = b.getName();
			
			String p1 = "";
			String p2 = "";
			for (int i = 0; i < name1.length(); i++) {
				p1 += PinyinHelper.toHanyuPinyinStringArray(name1.charAt(i))[0];
			}
			for (int i = 0; i < name2.length(); i++) {
				p2 += PinyinHelper.toHanyuPinyinStringArray(name2.charAt(i))[0];
			}
			
			return p1.compareTo(p2);
		});
		for (Student student : list) {
			System.out.println(student);
		}
	}
}
输出结果:
Student [name=榴莲, age=18, score=75.0]
Student [name=加菲猫, age=19, score=83.0]
Student [name=芒果, age=20, score=74.0]
Student [name=大魔王, age=20, score=84.0]
Student [name=穆桂英, age=21, score=65.0]
Student [name=张三丰, age=22, score=98.0]

Student [name=穆桂英, age=21, score=65.0]
Student [name=芒果, age=20, score=74.0]
Student [name=榴莲, age=18, score=75.0]
Student [name=加菲猫, age=19, score=83.0]
Student [name=大魔王, age=20, score=84.0]
Student [name=张三丰, age=22, score=98.0]

Student [name=大魔王, age=20, score=84.0]
Student [name=加菲猫, age=19, score=83.0]
Student [name=榴莲, age=18, score=75.0]
Student [name=芒果, age=20, score=74.0]
Student [name=穆桂英, age=21, score=65.0]
Student [name=张三丰, age=22, score=98.0]

用sort方法可以简化大量代码,不用再写一大堆c++的各种逻辑丰富的代码,非常实用,用途也非常广泛!!!谢谢品鉴,如有问题,希望能不吝批评!!!

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值