import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
/**
* create by 86159 on 2021/1/11
*/
public class ArraySort {
public static void main(String[] args) {
// 生命一个集合
List<Integer> list = new ArrayList<>();
list.add(7);
list.add(2);
list.add(102);
list.add(367);
list.add(-28);
//升序方式一
list.sort(Comparator.naturalOrder());
System.out.println(list);
// 升序方式二
list.sort(new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return Integer.compare(o1,o2);
}
});
System.out.println(list);
//倒序方式一
list.sort(new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return Integer.compare(o2,o1);
}
});
System.out.println(list);
//倒叙方式二:lamda表达式
list.sort((x,y) -> Integer.compare(y,x));
System.out.println(list);
}
}
运行结果如下:
[-28, 2, 7, 102, 367]
[-28, 2, 7, 102, 367]
[367, 102, 7, 2, -28]
[367, 102, 7, 2, -28]
想想,如果一个对象数组,要根据对象中的某一个属性对数组进行排序,该怎么处理呢?
package org.sang.model.entity;
/**
* @Author: chuxia0811
* @Date: 2021/1/18 22:37
* @Description :
*/
public class Student {
private String sno; //学号
private String name;//姓名
private Integer age; //年龄
public String getSno() {
return sno;
}
public void setSno(String sno) {
this.sno = sno;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"sno='" + sno + '\'' +
", name='" + name + '\'' +
", age=" + age +
'}';
}
public Student(String sno, String name, Integer age) {
this.sno = sno;
this.name = name;
this.age = age;
}
}
package org.sang.test;
import org.sang.model.entity.Student;
import java.util.Arrays;
import java.util.Comparator;
/**
* @Author: chuxia0811
* @Date: 2021/1/18 22:39
* @Description :
*/
public class ArraySort2 {
public static void main(String[] args) {
Student stu1 = new Student("001", "张三", 18);
Student stu2 = new Student("002", "李四", 23);
Student stu3 = new Student("003", "王五", 16);
Student stu4 = new Student("004", "Jhon", 32);
Student stu5 = new Student("005", "tom", 10);
Student stu6 = new Student("006", "alice", 53);
Student[] stus = new Student[6];
stus[0] = stu1;
stus[1] = stu2;
stus[2] = stu3;
stus[3] = stu4;
stus[4] = stu5;
stus[5] = stu6;
System.out.println("---------排序前:");
for (int i = 0;i<stus.length;i++){
System.out.println(stus[i].toString());
}
//排序
Arrays.sort(stus, new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
return o1.getAge().compareTo(o2.getAge());//根据age属性正序排序
}
});
System.out.println("---------排序后:");
for (int i = 0;i<stus.length;i++){
System.out.println(stus[i].toString());
}
}
}
数组中是student 对象,现在要根据age属性对数组进行排序,运行结果如下:
---------排序前:
Student{sno='001', name='张三', age=18}
Student{sno='002', name='李四', age=23}
Student{sno='003', name='王五', age=16}
Student{sno='004', name='Jhon', age=32}
Student{sno='005', name='tom', age=10}
Student{sno='006', name='alice', age=53}
---------排序后:
Student{sno='005', name='tom', age=10}
Student{sno='003', name='王五', age=16}
Student{sno='001', name='张三', age=18}
Student{sno='002', name='李四', age=23}
Student{sno='004', name='Jhon', age=32}
Student{sno='006', name='alice', age=53}