Java - how to sort an object in different ways: Arrays.sort (), Comparable <T>

Let's say that I have an array with objects, where I have several employees (objects). All of them have: int age , double salary . I want to sort this array so that my class implements Comparable <Employee> . I made a method:

 public int compareTo(Employee other) { return Double.compare(salary, other.salary); } 

And everything is in order, sorting works fine. But I sort of double salary . Now I want to sort int age , so now? I made a method:

 public int compareAge(Employee other) { return Integer.compare(age, other.age); } 

And how can I use Arrays.sort() with this? I want to be able to use both methods - sort by salary, sort by age. Thank you for your help.

+5
source share
2 answers

To implement several ways to sort the Employee link collection, you must create separate classes that implement Comparator<Employee> . So you can:

 public class EmployeeAgeComparator implements Comparator<Employee> { ... } public class EmployeeSalaryComparator implements Comparator<Employee> { ... } 

Then you simply pass the instance of the corresponding comparator to the Arrays.sort method.

Basically, a Comparable implementation Comparable good when there is one sort order, which is a reasonable default, but comparators allow you to separate the “things being compared” from the “thing doing the comparison”.

As a note, using double to represent currency values ​​(e.g. salaries) is a bad idea due to the way binary floating point works (e.g. not being able to represent exactly 0.1) ... use BigDecimal or store an integer cents (or any other currency unit that you use).

+9
source

You should use two Comparator classes instead of implementing Comparable .

In short, a class implementing Comparable will be comparable in one aspect to instances of this class.

A class implementing Comparator will be a comparator environment for some other class. This means that you can have multiple comparators to compare classes for different aspects. In addition, the Comparator class can be passed to a sorting method, such as Collections.sort() or Arrays.sort() , to provide precise control over the sorting order and can also be used to control the order of certain data structures, such as sorted sets or sorted maps.

To serve your purpose, you can create two Comparator classes, for example:

 class SalaryComparator implements Comparator<Employee> { int compare(Employee a, Employee b) { return Double.compare(a.salary, b.salary); } } class AgeComparator implements Comparator<Employee> { int compare(Employee a, Employee b) { return Integer.compare(a.age, b.age); } } 

And then when you call the sort method, you pass in the Comparator that you would like to use.

For example, if you have an ArrayList<Employee> list , and you want to sort it by salary, you can do something like:

  Collections.sort(list, new SalaryComparator()); // sort the list by salaries 

Or, if you have an Employee[] array and you want to sort it by age, for example:

 Arrays.sort(array, new AgeComparator()); // sort the array by age 
+5
source

Source: https://habr.com/ru/post/1203560/


All Articles