# 猜一猜

int[] ints = {50,1,4,8,3};
String [] strings = {"q","a","c"};
Arrays.sort(ints);

for (String val: strings) {
System.out.print(val + " ");
}

System.out.println();

for (int i = 0; i < ints.length; i++) {
System.out.print(ints[i]+" ");
}


q a c
1 3 4 8 50


# 但是，如果面试官问你：

1、Integer的源代码

public final class Integer extends Number implements Comparable<Integer> {

.....
.....

.....

public int compareTo(Integer anotherInteger) {
return compare(this.value, anotherInteger.value);
}
}


2、Short的源代码

public final class Short extends Number implements Comparable<Short> {

.....

.....
.....

public int compareTo(Short anotherShort) {
return compare(this.value, anotherShort.value);
}

}


3、String的源代码

public final class String
implements java.io.Serializable, Comparable<String>, CharSequence {

.....

.....

.....

public int compareTo(String anotherString) {
int len1 = value.length;
int len2 = anotherString.value.length;
int lim = Math.min(len1, len2);
char v1[] = value;
char v2[] = anotherString.value;

int k = 0;
while (k < lim) {
char c1 = v1[k];
char c2 = v2[k];
if (c1 != c2) {
return c1 - c2;
}
k++;
}
return len1 - len2;
}

}


# 试试真是如此吗？不信测试一下

## 1、Student类

package ComparableInterface;

/**
* @Auther: truedei
* @Date: 2020 /20-5-7 20:29
* @Description:
*/
public class Student implements Comparable<Student>{
private int age;

private String name;

public Student() {

}

public Student(int age, String name) {
this.age = age;
this.name = name;
}

public int getAge() {
return age;
}

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

public String getName() {
return name;
}

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

@Override
public String toString() {
return "Student{" +
"age=" + age +
", name='" + name + '\'' +
'}';
}

@Override
public int compareTo(Student o) {
return this.getAge() - o.getAge();
}
}



## 2、User类

package ComparableInterface;

/**
* @Auther: truedei
* @Date: 2020 /20-5-7 20:29
* @Description:
*/
public class User{
private int age;

private String name;

public User() {

}

public User(int age, String name) {
this.age = age;
this.name = name;
}

public int getAge() {
return age;
}

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

public String getName() {
return name;
}

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

@Override
public String toString() {
return "User{" +
"age=" + age +
", name='" + name + '\'' +
'}';
}

}


## 3、测试类

package ComparableInterface;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
* @Auther: truedei
* @Date: 2020 /20-5-7 20:29
* @Description:
*/
public class Test {

public static void main(String[] args) {

}
}


## 4、测试1 比较Student类

Student s1 = new Student(19,"张三");
Student s2 = new Student(18,"李四");

int i = s1.compareTo(s2);
if(i>=0){
System.out.println(s1.getName()+"年龄大");
}else{
System.out.println(s2.getName()+"年龄大");
}


张三年龄大


## 5、测试2 比较User类

        User u1 = new User(12,"王五");
User u2 = new User(22,"赵六");

int j = u1.compareTo(u2);
if(j>=0){
System.out.println(u1.getName()+"年龄大");
}else{
System.out.println(u2.getName()+"年龄大");
}


## 6、测试3 对Student类的数组排序

        Student s1 = new Student(19,"张三");
Student s2 = new Student(18,"李四");
Student s3 = new Student(2,"王五");

Student[] students = {s1,s2,s3};

System.out.println("排序前:");
for (Student st:students) {
System.out.println(st.toString());
}

System.out.println();

//进行排序
Arrays.sort(students);

System.out.println("排序后：");
for (Student st:students) {
System.out.println(st.toString());
}


排序前:
Student{age=19, name='张三'}
Student{age=18, name='李四'}
Student{age=2, name='王五'}

Student{age=2, name='王五'}
Student{age=18, name='李四'}
Student{age=19, name='张三'}


    @Override
public int compareTo(Student o) {
//        return this.getAge() - o.getAge();
return o.getAge()- this.getAge();
}


Student s1 = new Student(7,"张三");
Student s2 = new Student(18,"李四");
Student s3 = new Student(2,"王五");


排序前:
Student{age=7, name='张三'}
Student{age=18, name='李四'}
Student{age=2, name='王五'}

Student{age=18, name='李四'}
Student{age=7, name='张三'}
Student{age=2, name='王五'}

Process finished with exit code 0


## 7、测试4 对User类的数组排序

可以先猜测一下，可能排序吗？

        User u1 = new User(12,"王五");
User u2 = new User(22,"赵六");
User u3 = new User(15,"杂七");

User[] users = {u1,u2,u3};

System.out.println("排序前:");

for (User user:users) {
System.out.println(user.toString());
}

System.out.println();

//进行排序
Arrays.sort(users);

System.out.println("排序后：");
for (User user:users) {
System.out.println(user.toString());
}



排序前:
User{age=12, name='王五'}
User{age=22, name='赵六'}
User{age=15, name='杂七'}

Exception in thread "main" java.lang.ClassCastException: ComparableInterface.User cannot be cast to java.lang.Comparable
at java.util.ComparableTimSort.countRunAndMakeAscending(ComparableTimSort.java:320)
at java.util.ComparableTimSort.sort(ComparableTimSort.java:188)
at java.util.Arrays.sort(Arrays.java:1246)
at ComparableInterface.Test.main(Test.java:54)


没有实现Comparable接口的类的数组是无法使用Arrays.sort()进行排序的

## 8、改造 User类 进一步测试

1、给User类实现Comparable接口接口

### 1、需求如下

1、因为User类有年龄和姓名，年龄小的优先。
2、如果年龄相同，那么比较姓名，按姓名的ascii码值来排序，例如：a-b-c

### 2、修改User类

package ComparableInterface;

/**
* @Auther: truedei
* @Date: 2020 /20-5-7 20:29
* @Description:
*/
public class User implements Comparable<User>{
private int age;

private String name;

public User() {

}

public User(int age, String name) {
this.age = age;
this.name = name;
}

public int getAge() {
return age;
}

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

public String getName() {
return name;
}

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

@Override
public String toString() {
return "User{" +
"age=" + age +
", name='" + name + '\'' +
'}';
}

@Override
public int compareTo(User o) {
//年龄从小到大
if(this.getAge() - o.getAge() > 0){
return 1;
}else if(this.getAge() - o.getAge() == 0){
return this.getName().compareTo(o.getName());
}

return -1;
}
}


重点是:

1、可以看到年龄是从小到大的对比
2、如果this.年龄 - o.年龄 == 0 说明相等，那么就比较名字

@Override
public int compareTo(User o) {
//年龄从小到大
if(this.getAge() - o.getAge() > 0){
return 1;
}else if(this.getAge() - o.getAge() == 0){
return this.getName().compareTo(o.getName());
}

return -1;
}


### 3、测试

System.out.println("a="+(int)'a');
System.out.println("b="+(int)'b');
System.out.println("y="+(int)'y');


97<98<121
a<b<y

a=97
b=98
y=121


        User u1 = new User(12,"b");
User u2 = new User(22,"y");
User u3 = new User(22,"a");

User[] users = {u1,u2,u3};

System.out.println("排序前:");

for (User user:users) {
System.out.println(user.toString());
}

System.out.println();

//进行排序
Arrays.sort(users);

System.out.println("排序后：");
for (User user:users) {
System.out.println(user.toString());
}


排序前:
User{age=12, name='b'}
User{age=22, name='y'}
User{age=22, name='a'}

User{age=12, name='b'}
User{age=22, name='a'}
User{age=22, name='y'}


注意：最后送大家一套2020最新Java架构实战教程+大厂面试题库，点击此处 进来获取 一起交流进步哦！

05-13 149

03-24 706

03-12 516

03-13 585

05-16 8548

03-25 439

03-30 18万+

04-14 62万+

01-28 218

03-15 7万+

05-24 662

11-07 4193

05-27 61

04-10 8403

05-27 169

02-06 480

05-11 70

07-23 345

05-03 2997

#### JAVA使用Arrays.sort()降序排序需要注意的点

©️2019 CSDN 皮肤主题: 数字20 设计师: CSDN官方博客