使用 Java 的 DSA - 排序对象

可以使用 java.util.Arrays.sort() 方法轻松对 Java 对象进行排序。请考虑以下示例。

ObjectSortingDemo.java

package com.tutorialspoint.simplesort;

public class ObjectSortingDemo {

    public static void main(String[] args){
    
        /* String 对象数组 */
        String[] employees
        = new String[] {"Robert", "Paul","John","Micheal" };
        
        System.out.println("未排序数组 : "
        + Arrays.toString(employees));
        
        /* 按字典顺序对数组进行排序 */
        Arrays.sort(employees);
        
        System.out.println("已排序数组:"
        + Arrays.toString(employees));
        System.out.println();
    }
}

如果我们编译并运行上述程序,则会产生以下结果:

未排序数组: [Robert, Paul, John, Micheal]
已排序数组 : [John, Micheal, Paul, Robert]

使用 Comparable 接口

为了对对象进行排序,其类可以实现 java.lang.Comparable 接口。请考虑以下代码。

Employee.java

package com.tutorialspoint.simplesort;

public class Employee implements Comparable<Employee> {
    
   private int employeeId;
   private String name;
   private String department;

   public Employee (int employeeId,String name, String department){
      this.employeeId = employeeId;
      this.name = name;
      this.department = department;
   }

   @Override
   public int compareTo(Employee employee) {
      return employeeId - employee.employeeId;
   }

   public String getName(){
      return name;
   }

   public String getDepartment(){
      return department;
   }

   @Override
   public String toString() {
      return "
[ " + employeeId
         +"," + name
         +"," + department
         +" ]";
   }    
}

此处 Employee 类实现 java.lang.Comparable 接口并具有方法 compareTo()。Array.sort() 使用合并排序算法并使用此 compareTo() 方法比较两个对象,以便对作为参数传递的对象数组进行排序。

ObjectSortingDemo.java

public class ObjectSortingDemo {

   public static void main(String[] args){

      /* 使用 Arrays.sort() 方法对数组进行排序 */
      Employee[] employeesObjects = new Employee[] {
         new Employee(100, "Robert", "Finance"),
         new Employee(30, "Paul", "Finance"),
         new Employee(50, "John", "Finance"),
         new Employee(12, "Micheal", "Finance")
      };

      System.out.println("Unsorted Array : " 
         + Arrays.toString(employeesObjects));
      Arrays.sort(employeesObjects);  
      System.out.println();	  
      System.out.println("Sorted Array by id: " 
         + Arrays.toString(employeesObjects));      
   }
}   

如果我们编译并运行上述程序,则会产生以下结果 −

Unsorted Array : [
[ 100,Robert,Finance ],
[ 30,Paul,Finance ],
[ 50,John,Finance ],
[ 12,Micheal,Finance ]]

Sorted Array by id: [
[ 12,Micheal,Finance ],
[ 30,Paul,Finance ],
[ 50,John,Finance ],
[ 100,Robert,Finance ]]

使用 Comparator 接口

使用 java.util.Comparator 接口让我们能够精确控制对象的排序。正如我们在前面的例子中看到的,我们通过实现 java.lang.Comparable 接口设置了一个标准,即员工应该根据类中的 compareTo() 方法进行排序。使用 Comparator 类,如果类没有实现可比较的接口,我们可以在不修改类的情况下设置标准。请考虑以下代码。

Employee.java

package com.tutorialspoint.simplesort;

public class Employee {
    
   private int employeeId;
   private String name;
   private String department;

   public Employee (int employeeId,String name, String department){
      this.employeeId = employeeId;
      this.name = name;
      this.department = department;
   }   

   public String getName(){
      return name;
   }

   public String getDepartment(){
      return department;
   }

   @Override
   public String toString() {
      return "
[ " + employeeId
         +"," + name
         +"," + department
         +" ]";
   }    
}

定义一个比较器,可以根据姓名比较两个员工。

EmployeeNameComparator.java

package com.tutorialspoint.simplesort;

import java.util.Comparator;

public class EmployeeNameComparator implements Comparator<Employee> {
   @Override
   public int compare(Employee employee1, Employee employee2) {
      return employee1.getName().compareTo(employee2.getName());
   }    
}

此处 EmployeeNameComparator 类实现 java.util.Comparator 接口并具有方法 compare()。Array.sort() 使用合并排序算法并使用此 compare() 方法比较两个对象,以便对作为参数传递的对象数组进行排序。

ObjectSortingDemo.java

public class ObjectSortingDemo {

   public static void main(String[] args){

      /* 使用 Arrays.sort() 方法对数组进行排序 */
      Employee[] employeesObjects = new Employee[] {
         new Employee(100, "Robert", "Finance"),
         new Employee(30, "Paul", "Finance"),
         new Employee(50, "John", "Finance"),
         new Employee(12, "Micheal", "Finance")
      };

      System.out.println("Unsorted Array : " 
         + Arrays.toString(employeesObjects));
      Arrays.sort(employeesObjects,new EmployeeNameComparator());   
      System.out.println();
      System.out.println("Sorted Array by name: " 
         + Arrays.toString(employeesObjects));      
   }
}   

如果我们编译并运行上述程序,则会产生以下结果 −

Unsorted Array : [
[ 100,Robert,Finance ], 
[ 30,Paul,Finance ], 
[ 50,John,Finance ], 
[ 12,Micheal,Finance ]]

Sorted Array by name: [
[ 50,John,Finance ], 
[ 12,Micheal,Finance ], 
[ 30,Paul,Finance ], 
[ 100,Robert,Finance ]]