简体   繁体   中英

sorting an array of objects

I have a method that sorts an array of objects by first names which prints out fine. However when i want to print the original order of objects it still prints out the sorted version. What can i change to fix this?

Main method + other methods

String[] firstNames= {"Fred","John","Amir", "James","Bob","Jay","Amber"};
String[] lastNames = {"Bond","Kates","Memar", "White","Marley","Brown","Nogofski"};
String[] idNumbers = {"R111111","A222222","AB11111", "KR22121","V311133","L242434","P102432"};
String[] employeeNum = {"1111","2222","3333", "4444","5555","6666","7777"};

Employee[] list = new Employee[firstNames.length];
list = listOfEmployees(firstNames,lastNames,idNumbers); // create the list of employees in one array    
System.out.println("List of employees before sorting...\n");
printEmployeeList(list); //print the list of employees
sortWithFirstName(list);
printEmployeeList(list);


public static void printEmployeeList(Employee[] list){
    String employees = Arrays.toString(list).replace("[", "").replace("]", "").replace(",", "");
    System.out.println(employees);
    }

public static Employee[] sortWithFirstName(Employee[] list){
  System.out.println("Sorting the list with the first name");
  {
boolean swap;
//String temp; //change this too
do
{
    swap = false;
  for (int i = 0; i < (list.length - 1); i++)
{
    if (list[i].getFirstName().compareTo(list[i+1].getFirstName()) > 0) //if first number is greater then second then swap
    {
        //swap

        Employee temp = list[i];
        list[i] = list[i + 1];
        list[i + 1] = temp;
        swap = true;
    }
}

} while (swap == true);
printEmployeeList(list);
}
 return list;
}

Employee class

public static class Employee{
    private String firstName;
    private String lastName;
    private String idNumber;
    private String employeeNumber;
    private int employeeCount;

    /**
     * Constructor
     * @param firstName first name
     * @param lastName last name
     * @param idNumber id number 
     */
    public Employee(String firstName, String lastName, String idNumber){
        this.firstName = firstName;
        this.lastName = lastName;
        this.idNumber = idNumber;
        employeeCount = 0;
    }
    /**
     * Accessors here
     */

    public String getFirstName(){
        return firstName;
    }

    public String getLastName(){
        return lastName;
    }

    public String getIdNumber(){
        return idNumber;
    }

    public String getEmployeeNumber(){
        return employeeNumber;
    }

    // mutators here

    /**
     * @param firstName first name
     */
    public void setFirstName(String firstName){
        this.firstName = firstName;
    }

    /**
     * @param lastName last name
     */
    public void setLastName(String lastName){
        this.lastName = lastName;
    }

    /** 
     * @param idNumber id number
     */
    public void setIdNumber(String idNumber){
        this.idNumber = idNumber;
    }

    /**
     * @param employeeNumber employee number
     */
    public void setEmployeeNumber(String employeeNumber){
        this.employeeNumber = "";
    }
    @Override
    public String toString(){
        String result = "\nFirst name: " + getFirstName() + "\nLast name: " + getLastName()
                + "\nId number: " + getIdNumber() + "\nEmployee number: ";
        if(getEmployeeNumber() == null){
        return result + "No employee number has been assigned yet!\n";
    }
        return result + getEmployeeNumber() + "\n";
    }

}

我认为当您对原始数组进行排序时,您需要创建一个新的数组实例,而不是直接在其中进行操作,而是使用新实例,因此当您返回原始数组时,您会发现它就像创建一个排序之前一样从原始数组复制

Here you print the original order, then rearrange the list variable. If you print the list again, it will be rearranged because it is the same variable.

printEmployeeList(list); //print the list of employees
sortWithFirstName(list);
printEmployeeList(list);

You need to create a copy of the original list and rearrange that one instead (eg. listArranged), then if you want to print the original list you can access the first list variable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM