根据字符串中的数字对字符串进行排序,Java

Sorting a string based on a number in the string, Java

我正在尝试使用字符串实现归并排序,每个字符串包含一个数字,想根据该数字进行排序。这是我的代码,Employee.java 是我的构造函数,merg 是主要方法和 mergesort。它使用数字数组而不是字符串数组工作我收到以下错误:

reason: actual argument Employee[] cannot be converted to int[] by method invocation conversion
1 error


public class Employee
{
    private String name;
    private int idNumber;
    private String department;
    private String position;

    Employee(String n, int id, String dept, String pos)
    {
        name = n;
        idNumber = id;
        department = dept;
        position = pos;
    }
    Employee(int id)
    {
      idNumber = id;
    }
       public void setName(String n)
    {
        name = n;
    }
    public void setIdNumber(int id)
    {
        idNumber = id;
    }
    public void setDepartment(String dept)
    {
        department = dept;
    }
    public void setPosition(String pos)
    {
        position = pos;
    }
    public String getName()
    {
        return name;
    }
    public int getIdNumber()
    {
        return idNumber;
    }
 public String getDepartment()
    {
        return department;
    }
    public String getPosition()
    {
        return position;
    }
    public String toString()
    {
        String str = "|Employee name: " + name
            +"\n|Employee Identification: " + idNumber
            +"\n|Employee Department: " + department
            +"\n|Employee Postions: " + position;
        return str;
    }
}

合并文件

public class merg
{
   public static void main(String[] args)
   {
    Employee e1 = new Employee("Edward" , 3342, "Finance", "Consultant");
    Employee e2 = new Employee("Howard", 4452, "Human Resources", "Manager");
    Employee e3 = new Employee("Chelsea", 3354, "IT", "System Admin");
    Employee e4 = new Employee("Kevin", 2298, "Physical Plant" , "Janitor"); 


    Employee arr[] = new Employee[4];
    arr[0]=e1;
    arr[1]=e2;
    arr[2]=e3;
    arr[3]=e4;
    Employee arr1[] = arr;
    System.out.println("Before Merge Sort: ");
    for(int i=0; i<arr.length;i++){
        System.out.println(arr[i].toString());
    }
    System.out.println("After Merge Sort: ");
    MergeSort(arr1);
    for(int i=0;i<arr1.length;i++){
        System.out.println(arr1[i].toString());
      }
   }
   public static void Merge(int[] L, int[] R, int[] A)
   {
      int nL = L.length;
      int nR = R.length;
      int i = 0;
      int j = 0;
      int k = 0;

      while(i < nL && j < nR)
      {
         if(L[i] <= R[j])
         {
            A[k] = L[i];
            k++;
            i++;
         }
         else
         {
            A[k] = R[j];
            k++;
            j++;
         }

      }
      while( i < nL)
      {
         A[k] = L[i];
         i++;
         k++;
      }
      while( j < nR)
      {
         A[k] = R[j];
         j++;
         k++;
      }
   }
   public static void MergeSort(int[] A)
    {
      int n = A.length;
      int i, j,mid;
      if(n < 2)
         return;

      mid = n / 2;
      int[] left = new int[mid];
      int[] right = new int[n - mid];

      for(i = 0; i < mid; i++)
         left[i] = A[i];
      for(i = 0; i< n-mid; i++)
         right[i] = A[i+mid];
      MergeSort(left);
      MergeSort(right);
      Merge(left, right, A);
    }
}

首先我想问一下,你需要使用你自己的归并排序方法吗?如果不是,你应该让你的 class 实现 implements Comparator<Employee>

@Override
public int compare(Employee o1, Employee o2) {
    return Integer.compare(o1.getIdNumber(), o2.getIdNumber());
}

或使用

 Arrays.sort(arr, new Comparator<Employee>() {
    @Override
    public int compare(Employee o1, Employee o2) {
        Integer.compare(o1.getIdNumber(), o2.getIdNumber());
    }
});

顺便说一句 Arrays.sort 是一个 MergeSort

如果您必须使用自己的合并排序,您需要修改合并排序方法以采用 Employee[] 而不是 int[] 并使用 Employee[] 的实例 int[] 所以:

public static void MergeSort(Employee[] A)
 {
   int n = A.length;
   int i, j,mid;
   if(n < 2)
      return;

   mid = n / 2;
   Employee[] left = new Employee[mid];
   Employee[] right = new Employee[n - mid];

   for(i = 0; i < mid; i++)
      left[i] = A[i];
   for(i = 0; i< n-mid; i++)
      right[i] = A[i+mid];
   MergeSort(left);
   MergeSort(right);
   Merge(left, right, A);
 }

然后修改您的 Merge 以与 Employee[]

一起使用
public static void Merge(Employee[] L, Employee[] R, Employee[] A)
{
   int nL = L.length;
   int nR = R.length;
   int i = 0;
   int j = 0;
   int k = 0;

   while(i < nL && j < nR)
   {
      if(L[i].getIdNumber() <= R[j].getIdNumber())
      {
         A[k] = L[i];
         k++;
         i++;
      }
      else
      {
         A[k] = R[j];
         k++;
         j++;
      }
   }
   while( i < nL)
   {
      A[k] = L[i];
      i++;
      k++;
   }
   while( j < nR)
   {
      A[k] = R[j];
      j++;
      k++;
   }
}