-2

I want to sort the adding array in the code block below according to the integer value workinghour, but I get the information from mySQL and I need to display it in JTable. How can I sort?

public void monthOfEmployee() {
    model.setRowCount(0);
    ArrayList<Employee> employees = new ArrayList<Employee>();
    employees = process.showEmployee();

    if (employees != null) {
        for (Employee employee : employees) {
            Object[] adding = { employee.getID(), employee.getName(), employee.getSalary(),
                    employee.getWorkingHour() };

            model.addRow(adding);
        }
    }
}

SQL query

public ArrayList<Employee> showEmployee() {

    ArrayList<Employee> tried = new ArrayList<Employee>();
    try {
        statement = con.createStatement();
        String query = "Select * From employee";

        ResultSet rs = statement.executeQuery(query);

        while (rs.next()) {
            String iD = rs.getString("emp_ID");
            int workingHour = rs.getInt("working_hour");
            int salary = rs.getInt("salary");
            String name = rs.getString("emp_name");

            tried.add(new Employee(name, iD, salary, workingHour));

        }
        return tried;

    } catch (SQLException ex) {
        Logger.getLogger(Process.class.getName()).log(Level.SEVERE, null, ex);
        return null;
    }
}
SelimSezer
  • 29
  • 6
  • 1
    Does this answer your question? [How to sort an array of objects in Java?](https://stackoverflow.com/questions/18895915/how-to-sort-an-array-of-objects-in-java) – Thiyagu Jun 13 '21 at 11:15
  • 1
    [Sort ArrayList of custom Objects by property](https://stackoverflow.com/a/2784576/3890632) – khelwood Jun 13 '21 at 11:19

2 Answers2

0

Your Employee class needs to implement Comparable interface and provide a definition for compareTo method. You can sort on the basis of any field of your choice. In your case, its workingHour integer field.

Below might help you further. I have taken two approaches: One using TreeSet which is by default sorted and another using Java 8 stream.sorted.

public class Sortedtest
{
    public static void main(String[] args) {
        /** Method 1: having a treeset which is sorted itself */
        TreeSet<Employee> employees = new TreeSet<>();
        Employee employee1 = new Employee(3, "Raj", 20,9);
        Employee employee2 = new Employee(5, "Shyam", 18,8);
        Employee employee3 = new Employee(1, "Ram", 19,9);
        Employee employee4 = new Employee(4, "Sunil", 25,8);
        Employee employee5 = new Employee(2, "Ajay", 26,7);
        employees.add(employee1);
        employees.add(employee2);
        employees.add(employee3);
        employees.add(employee4);
        employees.add(employee5);

        // sorted  by themselves
        System.out.println("Sorted using Treeset: " + employees);


        /** Method 2: using Stream.sorted method */

        List<Employee> employeeList = Arrays.asList(employee1, employee2, employee3, employee4, employee5);
        List<Employee> sortedList = employeeList.stream().sorted().collect(Collectors.toList());
        System.out.println("Sorted using Stream: " + sortedList);
    }
}


class Employee implements Comparable<Employee> {
    private int id;
    private String name;
    private int salary;
    private int workingHour;

    @Override public String toString()
    {
        return "Employee{" + "id=" + id + ", name='" + name + '\'' + ", salary=" + salary + ", workingHour=" + workingHour + '}';
    }

    public Employee(int id, String name, int salary, int workingHour) {
        this.id = id;
        this.name = name;
        this.salary = salary;
        this.workingHour = workingHour;
    }
    @Override
    public int compareTo(Employee o) {
        if (this.workingHour> o.workingHour) {
            return 1;
        } else if (this.workingHour== o.workingHour) {
            return 0;
        } else {
            return -1;
        }
    }

}
KnockingHeads
  • 1,569
  • 1
  • 22
  • 42
0

If you can avoid using Object[], you could use this method, making use of Comparator class available from Java 8

List<Employee> employees = Arrays.asList(
                new Employee("One", 13),
                new Employee("Two", 8),
                new Employee("Three", 2),
                new Employee("Four", 11)
        );

employees.sort(Comparator.comparing(Employee::getWorkingHour));
Prebiusta
  • 477
  • 3
  • 14