How do I sort an ArrayList of objects by each object's integer parameter?
-
Say I have an ArrayList of Employee objects. These objects' parameters are String, int, and enum. So each element in the array is an object that looks like this: Employee Samuel = new Employee("Samuel", 56, Department.ACCOUNTING); After I've added the compare method for the integer type field in the Employee class, how do I sort the list by ascending integers?
-
Answer:
You can either implement Comparator and use that in java.util.Collections.sort to sort your list. public class Employee { ... } public class EmployeeComparator implements Comparator<Employee> { public int compare(Employee self, Employee other) { // I'm assuming your Employee.id is an Integer not an int. // If you'd like to use int, create an Integer before calling compareTo. return self.getId().compareTo(other.getId()); } } Collections.sort(employees, new EmployeeComparator()); // Or using an anonymous comparator. Collections.sort(employees, new Comparator<Employee>() { public int compare(Employee self, Employee other) { // I'm assuming your Employee.id is an Integer not an int. // If you'd like to use int, create an Integer before calling compareTo. return self.getId().compareTo(other.getId()); } }); Or, implement Comperable instead: public class Employee implements Comparable<Employee> { ... public int compareTo(Employee that) { return this.id.compareTo(that.id); } } Collections.sort(employees);
Soheil Hassas Yeganeh at Quora Visit the source
Other answers
I was A2A. As soon as I started writing my answer I saw answer which is exact replica of what I would have also written. Just to add few more points to the below answer,prefer using comparator instead of comparable.By Using a Comparable you are finalizing the default sorting behaviour for your class which may or may not be useful for others. When to use Comparable: Class should implement the comparable interface when it is the most natural way of sorting objects of that class,and anyone who wants to sort those objects would also do what is done in comparable implementation.Or to be specific use comparable when you want to define default sorting order for objects of a class. When to use Comparator(Prefered Method): When you can't decide upon the natural sorting for your class, then go for this option.As in your case one may want to sort Employee objects either by salary,age,Id etc, so for each of these sorting behaviour use comparator for each sorting criteria. PS:I think you should always refer http://stackoverflow.com/ for all such technical questions.Stack Overflow is a question and answer site for professional and enthusiast programmers containing library of detailed answers to every question about programming.
Yogendra Mishra
Many of the classes like String, Integer, Date etc. already implement comparable interface and for integer natural ordering is ascending so in your case you can use the compareTo method of the comparable interface already implemented by Integer class. Note that it has to be Integer not int. Now you can use either comparator or comparable. I'll suggest you can use Comparable and define the natural ordering for your own class as Employees in ascending order of age.... If you want to sort in any other way then use a Comparator. Using Comparable - Your Employee class should implement Comparable and provide implementation for compareTo method. public int compareTo(Employee o) { return this.getAge().compareTo(o.getAge()); } and then in the sorting logic - Collections.sort(empList); See more here - http://netjs.blogspot.in/2015/08/how-to-sort-arraylist-of-custom-objects-java.html
Anshudeep Bajpai
You can use a comparator and then implement its compareTo method for sorting The return value of the compare method decides the equality of the objects. If return value is a negative number â Object 1 is less than Object 2 If return value is a positive number â Object 1 is greater than Object 2 If return value is 0 â Object 1 is equal to Object 2 You can get more details here http://www.kscodes.com/java/sorting-arraylist-java-using-comparators/
Ketan Savant
Related Q & A:
- How can I sort a column in a DataGrid?Best solution by Stack Overflow
- How can I sort my data in alphabetic order?Best solution by Stack Overflow
- how do I sort dictionaries by value then by key?Best solution by Stack Overflow
- How can I sort strings in multiple languages?Best solution by Stack Overflow
- How do I sort out my emails?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.