# Comparable与Comparator，java中的排序与比较

1：比较和排序的概念

比较：两个实体类之间按>，=，<进行比较。

排序：在集合类中，对集合类中的实体进行排序。排序基于的算法基于实体类提供的比较函数。

基本型别都提供了默认的比较算法，如string提供了按字母进行比较，int提供了按整数大小进行比较。

2：Comparable与Comparator

but，光看概念没有任何意义，我们不知道这对代码来说意味着什么；

public class CollectorTest {
public static void main(String[] args) {

ArrayList<Student> students = new ArrayList<>();
for (Student student : students) {
System.out.printf("name:%s, age: %d \n", student.name, student.age);
}
}
}

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

public class CollectorTest {
public static void main(String[] args) {

ArrayList<Student> students = new ArrayList<>();
Collections.sort(students);
for (Student student : students) {
System.out.printf("name:%s, age: %d \n", student.name, student.age);
}
}
}

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

@Override
public int compareTo(Object o) {
Student student = (Student)o;
if (age> student.age)
{
return 1;
}
else if (age == student.age)
{
return 0;
}
else
{
return -1;
}
//return Integer.valueOf(age).compareTo(Integer.valueOf(student.age));
}
}

name:b, age: 1
name:c, age: 2
name:a, age: 3
name:d, age: 4

public class CollectorTest {
public static void main(String[] args) {

ArrayList<Student> students = new ArrayList<>();
//Collections.sort(students);
students.sort(new NameSorter());
for (Student student : students) {
System.out.printf("name:%s, age: %d \n", student.name, student.age);
}
}
}

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

@Override
public int compareTo(Object o) {
Student student = (Student)o;
if (age> student.age)
{
return 1;
}
else if (age == student.age)
{
return 0;
}
else
{
return -1;
}
//return Integer.valueOf(age).compareTo(Integer.valueOf(student.age));
}
}

class NameSorter implements Comparator{

@Override
public int compare(Object o1, Object o2) {
Student s1 =(Student) o1;
Student s2 =(Student) o2;
return s1.name.compareTo(s2.name);
}
}

name:a, age: 3
name:b, age: 1
name:c, age: 2
name:d, age: 4

3：Comparable与Comparator的泛型版本Comparable<T>与Comparator<T>

Student s1 =(Student) o1;
Student s2 =(Student) o2;

package com.zuikc.se.collectors;

/**
* 排序和比较器的用法，Comparable与Comparator，java中的排序与比较
* author:最课程（zuikc.com）
*/
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class CollectorTest {
public static void main(String[] args) {

ArrayList<Student> students = new ArrayList<>();
//Collections.sort(students);
students.sort(new NameSorter());
for (Student student : students) {
System.out.printf("name:%s, age: %d \n", student.name, student.age);
}
}
}

class Student implements Comparable<Student>{
public String name;
public int age;
public Student(String name, int age){
this.name = name;
this.age = age;
}

@Override
public int compareTo(Student student) {
if (age> student.age)
{
return 1;
}
else if (age == student.age)
{
return 0;
}
else
{
return -1;
}
//return Integer.valueOf(age).compareTo(Integer.valueOf(student.age));
}
}

class NameSorter implements Comparator<Student>{

@Override
public int compare(Student s1, Student s2) {
return s1.name.compareTo(s2.name);
}
}

|
1天前
|

＜八大排序＞万字详解(Java实现).插入排序、希尔排序、堆排序、快速排序、归并排序、计数排序...
＜八大排序＞万字详解(Java实现).插入排序、希尔排序、堆排序、快速排序、归并排序、计数排序
3 0
|
5天前
|
Java
【JAVA学习之路 | 提高篇】Java比较器(comparable) &&(comparator)
【JAVA学习之路 | 提高篇】Java比较器(comparable) &&(comparator)
13 5
|
11天前
|

Java大文件排序（有手就能学会），kafka面试题2024
Java大文件排序（有手就能学会），kafka面试题2024
25 1
|
13天前
|

【Java高阶数据结构】图补充-拓扑排序
【Java高阶数据结构】图补充-拓扑排序
17 1
|
13天前
|

8大Java排序方法(由简入繁)，有代码详解和原理指导
8大Java排序方法(由简入繁)，有代码详解和原理指导
34 0
|
13天前
|

Java排序：原理、实现与应用
【4月更文挑战第28天】本文探讨了Java中的排序算法，包括原理和实现。Java利用Comparator接口进行元素比较，通过Arrays和Collections类的sort方法对数组和列表进行排序。示例展示了使用这些方法的基本代码。此外，还讨论了冒泡排序算法和自定义排序场景，以适应不同需求。理解这些排序机制有助于提升程序效率。
15 1
|
13天前
|
Java
Java对关于两个地点的根据经纬度算出后排序
Java对关于两个地点的根据经纬度算出后排序
17 0
|
13天前
|
Java
【Java】通过Comparator比较器的方式给对象数组排序
【Java】通过Comparator比较器的方式给对象数组排序
19 0
|
13天前
|

18 0
|
13天前
|
Java

【2月更文挑战第21天】
103 3