ammf18
ammf18

Reputation: 95

Sort an list object in the same order of another array java

Hello I have I want to order a list of employee for department objects but I have the order in an array here the objects and the example

public class Employee {

    private String name;
    private int age;
    private double salary;
    private Department department;
    public Employee(String name, int age, double salary, Department department) {
        ...
    }

    // standard getters, setters and toString
 }

public class Department  {

    private Integer id;
    private String name;

    public Department(Integer id, String name) {
        ...
    }

    // standard getters, setters and toString
}

       Department[] departments = new Department[] {
                new Department(1, "Computing" ), new Department(2, "Human Resources"),
                new Department(3, "administration"), new Department(4, "operations"),
                new Department(5, "marketing"), new Department(6, "communication")
        };

  Employee[] employees = new Employee[] {
                new Employee("John", 23, 5000, departments[5]), new Employee("Steve", 26, 6000, departments[3]),
                new Employee("Frank", 33, 7000,departments[4]), new Employee("Earl", 43, 10000, departments[2]),
                new Employee("Jessica", 23, 4000, departments[1]), new Employee("Pearl", 33, 6000, departments[0])};


        String[] arrOrderDepartment = new String[]{"marketing", "Computing", "administration", "Human Resources", "communication", "operations"};

the bottom line is the employees ordered by the order of the department arrangement


employeesSortedByDepartment = [Employee{"Frank", 33, 7000, Department{id=5, name='marketing'}},Employee{ "Jessica", 23, 4000, Department{id=6, name='communication'} },Employee{"Steve", 26, 6000, Department{id=3, name='administration'} },Employee{"Earl", 43, 10000, Department{id=2, name='Human Resources'}},Employee{ "Pearl", 33, 6000, =Department{id=6, name='communication'} },Employee{ "John", 23, 5000, Department{id=4, name='operations'} }];


I have used something while working but it does not give me the expected result


 Collections.sort(department, new Comparator<String>(){
            public int compare(String left, String right) {
                return arrOrderDepartment[stringList.indexOf(left)] - arrOrder[stringList.indexOf(right)];
            }
        });

i am using java 6

thank you very much in what you can help me

Upvotes: 1

Views: 225

Answers (4)

ammf18
ammf18

Reputation: 95

thank you for your answer, yesterday from both analyzing and testing I got the solution contains something similar to the answer Arvind Kumar Avinash! gave me here I put the code

public class Main {

    public static void main(String[] args) {

        Department[] departments = new Department[] {
                new Department(1, "Computing" ), new Department(2, "Human Resources"),
                new Department(3, "administration"), new Department(4, "operations"),
                new Department(5, "marketing"), new Department(6, "communication"),
                new Department(7, "shopping"), new Department(6, "Inventory")
        };

        final Employee[] employees = new Employee[] {
                new Employee("John", 23, 5000, departments[5]), new Employee("Steve", 26, 6000, departments[3]),
                new Employee("Frank", 33, 7000,departments[4]), new Employee("Earl", 43, 10000, departments[2]),
                new Employee("Jessica", 23, 4000, departments[1]), new Employee("Pearl", 33, 6000, departments[0]),
                new Employee("Pearl", 33, 6000, departments[6]), new Employee("Pearl", 33, 6000, departments[7])
        };


       final String[] arrOrderDepartment = new String[]{"operations", "Computing", "administration", "Inventory", "Human Resources", "communication", "marketing"};

        final List<Employee> employeeList = Arrays.asList(employees);
        List<Employee> copyEmployeeList = Arrays.asList(employees);

        Collections.sort(copyEmployeeList, new Comparator<Employee>(){

            public int compare(Employee o1, Employee o2)
            {
                String a = getDepartmentOfList(o1.getDepartment().getName(), employeeList);
                String b = getDepartmentOfList(o2.getDepartment().getName(), employeeList);

              return  indexOf(a, arrOrderDepartment) - indexOf(b, arrOrderDepartment);
            }
        });

        System.out.println(copyEmployeeList);
    }

    private static String getDepartmentOfList(String name, List<Employee> employeeList) {
        for (Employee employee: employeeList) {
            if (name.equalsIgnoreCase(employee.getDepartment().getName())){
                return employee.getDepartment().getName();
            }
        }
        return null;
    }

    private static int indexOf( String c , String[] arr ) {
        for( int i = 0 ; i < arr.length ; i++ ) {
            if( arr[i].equalsIgnoreCase(c) ) {
                return i;
            }
        }
        return -1;
    }
}

add other departments to see how the code behaved and add it firstadd other departments to see how the code behaved and add it first

Upvotes: 0

Arvind Kumar Avinash
Arvind Kumar Avinash

Reputation: 79530

You can do it as follows:

public class Main {
    public static void main(String[] args) {
        Department[] departments = new Department[] { new Department(1, "Computing"),
                new Department(2, "Human Resources"), new Department(3, "administration"),
                new Department(4, "operations"), new Department(5, "marketing"), new Department(6, "communication") };

        Employee[] employees = new Employee[] { new Employee("John", 23, 5000, departments[5]),
                new Employee("Steve", 26, 6000, departments[3]), new Employee("Frank", 33, 7000, departments[4]),
                new Employee("Earl", 43, 10000, departments[2]), new Employee("Jessica", 23, 4000, departments[1]),
                new Employee("Pearl", 33, 6000, departments[0]) };

        String[] arrOrderDepartment = new String[] { "marketing", "Computing", "administration", "Human Resources",
                "communication", "operations" };
        Employee[] employeesSortedByDepartment = new Employee[employees.length];
        for (int i = 0; i < arrOrderDepartment.length; i++) {
            employeesSortedByDepartment[i] = getEmployeeByDeptId(employees,
                    findDeptIdByDeptName(departments, arrOrderDepartment[i]));
        }
        for (Employee employee : employeesSortedByDepartment) {
            System.out.println(employee);
        }
    }

    static int findDeptIdByDeptName(Department[] departments, String departmentName) {
        for (int i = 0; i < departments.length; i++) {
            if (departments[i].getName().equalsIgnoreCase(departmentName)) {
                return departments[i].getId();
            }
        }
        return -1;
    }

    static Employee getEmployeeByDeptId(Employee[] employees, int id) {
        for (Employee employee : employees) {
            if (employee.getDepartment().getId() == id) {
                return employee;
            }
        }
        return null;
    }
}

Output:

Employee [name=Frank, age=33, salary=7000.0, department=Department [id=5, name=marketing]]
Employee [name=Pearl, age=33, salary=6000.0, department=Department [id=1, name=Computing]]
Employee [name=Earl, age=43, salary=10000.0, department=Department [id=3, name=administration]]
Employee [name=Jessica, age=23, salary=4000.0, department=Department [id=2, name=Human Resources]]
Employee [name=John, age=23, salary=5000.0, department=Department [id=6, name=communication]]
Employee [name=Steve, age=26, salary=6000.0, department=Department [id=4, name=operations]]

Upvotes: 2

Andrey Rosa
Andrey Rosa

Reputation: 91

I think you can solve this with comparable fom Java 8 (I believe).

Here is the oracle document at this link. https://docs.oracle.com/javase/8/docs/api/java/lang/Comparable.html

And this link has an example of your question. How to sort an array of objects in Java?

Upvotes: 0

Joni
Joni

Reputation: 111349

Since your array of departments is already in order 1-6, you don't need to "sort" - what you need to do is apply the order that you want.

int[] arrOrderDepartment = {5, 1, 3, 2, 6, 4};

Department[] sortedDepartment = new Department[6];
for (int i = 0; i < 6; i++) {
    sortedDepartment[i] = department[arrOrderDepartment[i] - 1]
}

Now sortedDepartment[0] will point to the department 5, and sortedDepartment[1] will point to the department 1, and so on.

Upvotes: 0

Related Questions