创建实体类student,添加setter和getter方法
package Java.Sort.entity;
public class Student {
private String name;
private int score;
private int height;
private String college;
private String address;
public Student() {
}
public Student(String name, int score, int height, String college, String address) {
this.name = name;
this.score = score;
this.height = height;
this.college = college;
this.address = address;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public String getCollege() {
return college;
}
public void setCollege(String college) {
this.college = college;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", score=" + score +
", height=" + height +
", major='" + college + '\'' +
", address='" + address + '\'' +
'}';
}
}
1.单属性变量List排序
1.1按照变量属性升序,降序排序
public class singleVariableSort1 {
private void singleVariableSort1(){
List<Integer> list = Arrays.asList(10,1,6,4,8,7,9,3,2,5);
System.out.println("原始数据:");
list.forEach(n ->{System.out.print(n+", ");});
System.out.println("");
System.out.println("升序排列:");
Collections.sort(list);
list.forEach(n ->{System.out.print(n+", ");});
System.out.println("");
System.out.println("降序排列:");
Collections.reverse(list);
list.forEach(n ->{System.out.print(n+", ");});
}
public static void main(String[] args) {
new singleVariableSort1().singleVariableSort1();
}
}
1.2按照自定义的顺序排序
public class singleVariableSort2 {
private void singleVariableSort2(){
List<String> list = Arrays.asList("北京","上海","北京","广州","广州","上海","北京","上海");
List<String> sortRule=Arrays.asList("北京","上海","广州");
System.out.println("原始数据");
list.forEach(n->{System.out.print(n+", ");});
System.out.println("");
System.out.println("排序规则");
sortRule.forEach(n->{System.out.print(n+", ");});
System.out.println("");
System.out.println("正序排序");
Collections.sort(list, new Comparator<String>() {
public int compare(String o1, String o2) {
int io1=sortRule.indexOf(o1);
int io2=sortRule.indexOf(o2);
return io1-io2;
}
});
list.forEach(n->{System.out.print(n+", ");});
System.out.println("");
System.out.println("倒序排序");
Collections.sort(list, new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
int io1= sortRule.indexOf(o1);
int io2= sortRule.indexOf(o2);
return io2-io1;
}
});
list.forEach(n->{System.out.print(n+", ");});
}
public static void main(String[] args) {
new singleVariableSort2().singleVariableSort2();
}
}
2.对象List排序
2.1按照对象单属性升序,降序排序
public class entitySort1 {
private void entitySort1() {
List<Student> students = new ArrayList<>();
students.add(new Student("张三",90,180,"电气学院","北京"));
students.add(new Student("李四",80,165,"计算机学院","上海"));
students.add(new Student("王五",91,170,"财经学院","上海"));
students.add(new Student("赵明",80,182,"计算机学院","北京"));
students.add(new Student("钱海",75,181,"计算机学院","广州"));
students.add(new Student("孙理",82,172,"财经学院","上海"));
students.add(new Student("周伟",90,168,"电气学院","广州"));
students.add(new Student("郑亮",80,178,"财经学院","广州"));
System.out.println("原始数据:");
students.forEach(s ->{
System.out.println(s.getName()+" "+ s.getScore()+" "+s.getHeight()+" "+ s.getCollege()+""+s.getAddress());});
System.out.println("按照分数升序排序:");
students.sort(Comparator.comparing(Student::getScore));
students.forEach(s ->{
System.out.println(s.getName()+" "+ s.getScore()+" "+s.getHeight()+" "+ s.getCollege()+""+s.getAddress());});
System.out.println("按照分数降序排序:");
students.sort(Comparator.comparing(Student::getScore).reversed());
students.forEach(s ->{
System.out.println(s.getName()+" "+ s.getScore()+" "+s.getHeight()+" "+s.getCollege()+""+s.getAddress());});
}
public static void main(String[] args) {
new entitySort1().entitySort1();
}
}
2.2按照对象多属性升序,降序排序
public class entitySort2 {
private void entitySort2(){
List<Student> students = new ArrayList<>();
students.add(new Student("张三",90,180,"电气学院","北京"));
students.add(new Student("李四",80,165,"计算机学院","上海"));
students.add(new Student("王五",91,170,"财经学院","上海"));
students.add(new Student("赵明",80,182,"计算机学院","北京"));
students.add(new Student("钱海",75,181,"计算机学院","广州"));
students.add(new Student("孙理",82,172,"财经学院","上海"));
students.add(new Student("周伟",90,168,"电气学院","广州"));
students.add(new Student("郑亮",80,178,"财经学院","广州"));
System.out.println("原始数据:");
students.forEach(s ->{
System.out.println(s.getName()+" "+ s.getScore()+" "+s.getHeight()+" "+s.getCollege()+""+s.getAddress());});
System.out.println("按照分数降序排序,当分数相同时, 按照身高升序排序");
students.sort(Comparator.comparing(Student::getScore).reversed().thenComparing(Student::getHeight));
students.forEach(s ->{
System.out.println(s.getName()+" "+ s.getScore()+" "+s.getHeight()+" "+s.getCollege()+""+s.getAddress());});
System.out.println("按照分数降序排序,当分数相同时, 按照身高降序排序");
Collections.sort(students, new Comparator<Student>()
{
public int compare(Student student1, Student student2)
{
if(student1.getScore()==(student2.getScore())){
return student2.getHeight() - student1.getHeight();
}else{
return student2.getScore() - student1.getScore();
}
}
});
students.forEach(s ->{
System.out.println(s.getName()+" "+ s.getScore()+" "+s.getHeight()+" "+s.getCollege()+""+s.getAddress());});
}
public static void main(String[] args) {
new entitySort2().entitySort2();;
}
}
2.3按照对象自定义单属性的顺序排序
public class entitySort3 {
private void entitySort3(){
List<Student> students = new ArrayList<>();
students.add(new Student("张三",90,180,"电气学院","北京"));
students.add(new Student("李四",80,165,"计算机学院","上海"));
students.add(new Student("王五",91,170,"财经学院","上海"));
students.add(new Student("赵明",80,182,"计算机学院","北京"));
students.add(new Student("钱海",75,181,"计算机学院","广州"));
students.add(new Student("孙理",82,172,"财经学院","上海"));
students.add(new Student("周伟",90,168,"电气学院","广州"));
students.add(new Student("郑亮",80,178,"财经学院","广州"));
students.forEach(s ->{System.out.println(s.getName()+" "+s.getScore()+" "+s.getHeight()+" "+s.getCollege()+" "+s.getAddress());});
System.out.println("自定义按照地区(北京,上海,广州)排序:");
List<String> addressOrder= Arrays.asList("北京","上海","广州");
Collections.sort(students, new Comparator<Student>() {
@Override
public int compare(Student s1, Student s2) {
int io1= addressOrder.indexOf(s1.getAddress());
int io2= addressOrder.indexOf(s2.getAddress());
return io1-io2;
}
});
students.forEach(s->{System.out.println(s.getName()+" "+ s.getScore()+" "+s.getHeight()+" "+s.getCollege()+" "+s.getAddress());});
}
public static void main(String[] args) {
new entitySort3().entitySort3();
}
}
3、对日期进行排序
package Java.Sort;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
public class DateSort {
public static void main(String[] args) {
ArrayList<String> timeList = new ArrayList<String>();
timeList.add("2013-7-20 20:20");
timeList.add("2013-7-21 10:20");
timeList.add("2013-7-25 20:20");
timeList.add("2013-7-22 20:20");
timeList.add("2013-7-23 20:20");
timeList.add("2013-7-24 20:21");
timeList.add("2013-7-25 20:20");
System.out.println(timeList);
SimpleDateFormat format = new SimpleDateFormat("yyyy-M-dd HH:mm");
ArrayList<Date> dateList = new ArrayList<Date>();
for (String str : timeList) {
try {
dateList.add(format.parse(str));
} catch (ParseException e) {
e.printStackTrace();
}
}
System.out.println("排序前:");
for(Date d:dateList){
System.out.println(format.format(d));
}
Date tempDate = null;
for (int i = dateList.size()- 1; i > 0; --i) {
for (int j = 0; j < i; ++j) {
if(dateList.get(j+1).after(dateList.get(j))){
tempDate = dateList.get(j);
dateList.set(j, dateList.get(j+1));
dateList.set(j+1, tempDate);
}else{
}
}
}
System.out.println("排序后:");
for(Date d:dateList){
System.out.println(format.format(d));
}
}
}
4、使用stream流
public class demo {
public static void main(String[] args) {
List<Student> list = new ArrayList<>();
list.add(new Student("张三", 90, 180, "电气学院", "北京"));
list.add(new Student("李四", 80, 165, "计算机学院", "上海"));
list.add(new Student("王五", 91, 170, "财经学院", "上海"));
list.add(new Student("赵明", 80, 182, "计算机学院", "北京"));
list.add(new Student("钱海", 75, 181, "计算机学院", "广州"));
list.add(new Student("孙理", 82, 172, "财经学院", "上海"));
list.add(new Student("周伟", 90, 168, "电气学院", "广州"));
list.add(new Student("郑亮", 80, 178, "财经学院", "广州"));
List<Student> collect = list.stream().filter(student -> student.getScore() > 80).collect(Collectors.toList());
collect.forEach(System.out::println);
}
}