数组有一个工具类Arrays,里面有一个静态方法sort(),可以实现排序,不过他只能升序。可以使用Comparator.reverseOrder进行降序。
如果自己写的引用对象想实现排序功能需要实现Comparable接口重写compareTo方法,即可排序。
用户实体类:
package com.company.sort;
import java.util.Objects;
/**
*
* 类说明: 学生实体类实现Comparable进行排序
* 作者: Guo.shiLin
* 日期: 2018\7\27 0027
*
* @version 1.0
*/
public class Student implements Comparable {
private String name;
private int no;
public Student(String name, int no) {
this.name = name;
this.no = no;
}
public String getName() {
return name;
}
public int getNo() {
return no;
}
public void setName(String name) {
this.name = name;
}
public void setNo(int no) {
this.no = no;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", no=" + no +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student student = (Student) o;
return no == student.no &&
Objects.equals(name, student.name);
}
@Override
public int hashCode() {
return Objects.hash(name, no);
}
@Override
public int compareTo(Object o) {
//根据姓名进行排序,按字母顺序。
return this.getName().compareTo(((Student) o).getName());
}
}
数组排序代码如下:
package com.company.sort;
import java.util.Arrays;
import java.util.Comparator;
/**
*
* 类说明: 数组的排序
* 作者: Guo.shiLin
* 日期: 2018\7\27 0027
*
* @version 1.0
*/
public class ArraysSort {
public static void main(String[] args) {
Student[] student = new Student[4];
Student student1 = new Student("A", 10);
student[0] = student1;
Student student2 = new Student("v",13);
student[1] = student2;
Student student3 = new Student("d",15);
student[2] = student3;
Student student4 = new Student("b", 9);
student[3] = student4;
System.out.println("默认:" + Arrays.toString(student));
Arrays.sort(student);
System.out.println("升序:" + Arrays.toString(student));
Arrays.sort(student, Comparator.reverseOrder());
System.out.println("降序:" + Arrays.toString(student));
}
}
代码如下:
package com.company.sort;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
*
* 类说明: 实现ArrayList排序
* 作者: Guo.shiLin
* 日期: 2018\7\27 0027
*
* @version 1.0
*/
public class ArrayListSort {
public static void main(String[] args) {
List list = new ArrayList <>();
list.add(new Student("A", 12));
list.add(new Student("E", 12));
list.add(new Student("D", 12));
list.add(new Student("B", 12));
//调用Collections工具类中的sort排序方法。升序
System.out.println("默认:" + list);
Collections.sort(list);
System.out.println("升序:" + list);
//降序
Collections.sort(list, Collections.reverseOrder());
System.out.println("降序:" + list);
}
}
代码如下:
package com.company.sort;
import java.util.*;
/**
*
* 类说明: 利用Collections集合工具类实现排序
* 作者: Guo.shiLin
* 日期: 2018\7\27 0027
*
* @version 1.0
*/
public class HashSetSort {
public static void main(String[] args) {
Set set = new HashSet <>();
set.add(12);
set.add(34);
set.add(23);
set.add(40);
System.out.println("默认:" + set);
//因为Collections工具类里面的排序方法里面得传个list集合,所有得先把set转为list
List list = new ArrayList(set);
//升序
Collections.sort(list);
System.out.println("升序:" + list);
//降序
Collections.sort(list, Collections.reverseOrder());
System.out.println("降序:" + list);
System.out.println("下面是自己写的一个引用对象,然后进行复制排序。");
Set students = new HashSet <>();
Student student = new Student("C",12);
students.add(student);
Student student1 = new Student("A",11);
students.add(student1);
Student student2 = new Student("D",56);
students.add(student2);
Student student3 = new Student("B",22);
students.add(student3);
System.out.println("默认:" + students);
List slist = new ArrayList(students);
Collections.sort(slist);
System.out.println("升序:" + slist);
Collections.sort(slist, Collections.reverseOrder());
System.out.println("降序:" + slist);
}
}
TreeSet实现sortedSet接口可以进行排序。默认排序是升序,也可以自定义排序方式。
代码如下:
package com.company.sort;
import java.util.Collections;
import java.util.Comparator;
import java.util.TreeSet;
/**
*
* 类说明: TreeSet是有序集合,默认是升序。也可以自定义排序。
* 作者: Guo.shiLin
* 日期: 2018\7\27 0027
*
* @version 1.0
*/
public class TreeSetSort {
public static void main(String[] args) {
TreeSet treeSet = new TreeSet <>();
treeSet.add(12);
treeSet.add(23);
treeSet.add(10);
treeSet.add(34);
//默认升序
System.out.println("升序:" + treeSet);
//自定义降序
TreeSet trees = new TreeSet <>((o1, o2) -> o2 - o1);
trees.add(12);
trees.add(23);
trees.add(10);
trees.add(34);
System.out.println("降序:" + trees);
System.out.println("下面是自己写的一个引用对象,然后进行复制排序。");
TreeSet students = new TreeSet <>();
Student student = new Student("C",12);
students.add(student);
Student student1 = new Student("A",11);
students.add(student1);
Student student2 = new Student("D",56);
students.add(student2);
Student student3 = new Student("B",22);
students.add(student3);
System.out.println("升序:" + students);
TreeSet treeSet1 = new TreeSet <>((o1, o2) -> o2.getName().compareTo(o1.getName()));
Student student4 = new Student("C",12);
treeSet1.add(student);
Student student5 = new Student("A",11);
treeSet1.add(student1);
Student student6 = new Student("D",56);
treeSet1.add(student2);
Student student7 = new Student("B",22);
treeSet1.add(student3);
System.out.println("降序:" + treeSet1);
}
}