sort排序
在java中,可以使用sort方法快捷的进行排序。
数组的sort排序
- sort方法时Arrays类中的静态方法。可以直接利用类名进行调用。
Arrays类中sort的常用方法:
static void sort(type [] a, int fromIndex, int toIndex)
对指定数组的指定范围按数字升序进行排序。
type 可以指定为int,float,double,long,byte等。
a - 要排序的数组
fromIndex - 要排序的第一个元素的索引(包括)
toIndex - 要排序的最后一个元素的索引(不包括)
static void sort(type [] a)
对指定的 type型数组按数字升序进行排序。
默认为升序排列
for example
import java.util.Arrays;
import java.util.Scanner;
public class test {
public static void main(String[] args) {
Scanner scanner= new Scanner(System.in);
int n = scanner.nextInt();
long num[] =new long [n];
for(int i=0;i<n;i++){
num[i] = scanner.nextLong();
}
Arrays.sort(num,0,n);
for(int i=0;i<n;i++)
System.out.print(num[i]+" ");
}
}
输入:
5
1 3 6 2 4
输出:
1 2 3 4 6
利用sort函数对自定义对象的数组进行排序
对自定义的对象的数组进行sort排序,一般需要用到Comparable或是Comparator接口。首先在这里总结一下这两个接口的区别:
- Comparable可以认为是一个内比较器,实现了Comparable接口的类有一个特点,就是这些类是可以和自己比较的,至于具体和另一个实现了Comparable接口的类如何比较,则依赖compareTo方法的实现,compareTo方法也被称为自然比较方法。
compareTo方法的返回值是int,有三种情况:
1、比较者大于被比较者(也就是compareTo方法里面的对象),那么返回正整数
2、比较者等于被比较者,那么返回0
3、比较者小于被比较者,那么返回负整数
for example:
对一些学生根据总分成绩排序输出,总成绩相同时,根据语文成绩高低排序,如果语文成绩也相同时,依次按照数学,英语成绩进行排序。
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;
import javax.swing.plaf.synth.SynthStyle;
//外比较器
class cmp implements Comparator<Student>{
// 比较规则
@Override
public int compare(Student o1, Student o2) {
if(o1.getTotalScore()!=o2.getTotalScore())
return o1.getTotalScore()>o2.getTotalScore()?-1:1;
else if(o1.getChineseScore()!=o2.getChineseScore())
return o1.getChineseScore()>o2.getChineseScore()?-1:1;
else if(o1.getMathScore()!=o2.getMathScore())
return o1.getMathScore()>o2.getMathScore()?-1:1;
else
return o1.getEnglishScore()>o2.getEnglishScore()?-1:1;
}
}
class Student {
private String name ;
private double totalScore;
private double chineseScore;
private double mathScore;
private double englishScore;
//get 和set方法
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getTotalScore() {
return totalScore;
}
public void setTotalScore(double totalScore) {
this.totalScore = totalScore;
}
public double getChineseScore() {
return chineseScore;
}
public void setChineseScore(double chineseScore) {
this.chineseScore = chineseScore;
}
public double getMathScore() {
return mathScore;
}
public void setMathScore(double mathScore) {
this.mathScore = mathScore;
}
public double getEnglishScore() {
return englishScore;
}
public void setEnglishScore(double englishScore) {
this.englishScore = englishScore;
}
public Student(){}
public Student(String name ,double chineseScore,double mathScore,double englishScore){
this.name =name;
this.totalScore=chineseScore+mathScore+englishScore;
this.chineseScore =chineseScore;
this.mathScore=mathScore;
this.englishScore=englishScore;
}
@Override
public String toString() {
return name + " 总分:"+totalScore+" 语文: "+chineseScore+" 数学: "+mathScore+" 英语 :"+englishScore;
}
}
public class test {
public static void main(String[] args) {
Scanner sc =new Scanner(System.in);
int n= sc.nextInt();
Student stu[] = new Student[n];
for(int i=0;i<n;i++){
String name;
name =sc.next();
double chineseScore, mathScore, englishScore;
chineseScore =sc.nextDouble();
mathScore =sc.nextDouble();
englishScore =sc.nextDouble();
stu[i] = new Student(name, chineseScore, mathScore, englishScore);
}
Arrays.sort(stu,new cmp());
for(int i=0;i<n;i++){
System.out.println(stu[i].toString());
}
}
}
输入:
3
小王 120 110 135
小李 115 135 116
小张 122 126 131
输出:
小张 总分:379.0 语文: 122.0 数学: 126.0 英语 :131.0
小李 总分:366.0 语文: 115.0 数学: 135.0 英语 :116.0
小王 总分:365.0 语文: 120.0 数学: 110.0 英语 :135.0
当然为了写起来方便,外比较器可以这样写:
Arrays.sort(stu,new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
if(o1.getTotalScore()!=o2.getTotalScore())
return o1.getTotalScore()>o2.getTotalScore()?-1:1;
else if(o1.getChineseScore()!=o2.getChineseScore())
return o1.getChineseScore()>o2.getChineseScore()?-1:1;
else if(o1.getMathScore()!=o2.getMathScore())
return o1.getMathScore()>o2.getMathScore()?-1:1;
else
return o1.getEnglishScore()>o2.getEnglishScore()?-1:1;
}
});
集合的sort排序
- 对于常用的vector,list等也可以利用sort进行排序:
(1)list中的对象已经实现了Comparable接口,说明list中的那种对象是可比较的类型,支持自己和自己比较的,那么可以直接调用 Collections.sort(list);进行排序。例如常见的Integer,Float,Double类型等。
(2)如果list中的类没有实现Comparable接口,则需要借助外比较器,实现Comparator接口的类的对象
for example
例子和上面实现的相同,只是用的是vector存放student对象,然后排序。
import java.awt.List;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.Scanner;
import java.util.Vector;
class cmp implements Comparator<Student>{
@Override
public int compare(Student o1, Student o2) {
if(o1.getTotalScore()!=o2.getTotalScore())
return o1.getTotalScore()>o2.getTotalScore()?-1:1;
else if(o1.getChineseScore()!=o2.getChineseScore())
return o1.getChineseScore()>o2.getChineseScore()?-1:1;
else if(o1.getMathScore()!=o2.getMathScore())
return o1.getMathScore()>o2.getMathScore()?-1:1;
else
return o1.getEnglishScore()>o2.getEnglishScore()?-1:1;
}
}
class Student {
private String name ;
private double totalScore;
private double chineseScore;
private double mathScore;
private double englishScore;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getTotalScore() {
return totalScore;
}
public void setTotalScore(double totalScore) {
this.totalScore = totalScore;
}
public double getChineseScore() {
return chineseScore;
}
public void setChineseScore(double chineseScore) {
this.chineseScore = chineseScore;
}
public double getMathScore() {
return mathScore;
}
public void setMathScore(double mathScore) {
this.mathScore = mathScore;
}
public double getEnglishScore() {
return englishScore;
}
public void setEnglishScore(double englishScore) {
this.englishScore = englishScore;
}
public Student(){}
public Student(String name ,double chineseScore,double mathScore,double englishScore){
this.name =name;
this.totalScore=chineseScore+mathScore+englishScore;
this.chineseScore =chineseScore;
this.mathScore=mathScore;
this.englishScore=englishScore;
}
@Override
public String toString() {
return name + " 总分:"+totalScore+" 语文: "+chineseScore+" 数学: "+mathScore+" 英语 :"+englishScore;
}
}
public class test {
public static void main(String[] args) {
Scanner sc =new Scanner(System.in);
int n= sc.nextInt();
Vector<Student> vc =new Vector<Student>();
for(int i=0;i<n;i++){
String name;
name =sc.next();
double chineseScore, mathScore, englishScore;
chineseScore =sc.nextDouble();
mathScore =sc.nextDouble();
englishScore =sc.nextDouble();
vc.addElement( new Student(name, chineseScore, mathScore, englishScore));
}
Collections.sort(vc,new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
if(o1.getTotalScore()!=o2.getTotalScore())
return o1.getTotalScore()>o2.getTotalScore()?-1:1;
else if(o1.getChineseScore()!=o2.getChineseScore())
return o1.getChineseScore()>o2.getChineseScore()?-1:1;
else if(o1.getMathScore()!=o2.getMathScore())
return o1.getMathScore()>o2.getMathScore()?-1:1;
else
return o1.getEnglishScore()>o2.getEnglishScore()?-1:1;
}
});
Iterator<Student> iterator = vc.iterator();
while(iterator.hasNext()){
System.out.println(iterator.next().toString());
}
}
}