Sorting an array of type object using the Arrays.sort () method

I know how to sort an array of objects using the Arrays.sort () method as follows.

Arrays.sort(array of primitive type); Arrays.sort(array of primitive type, from, to); Arrays.sort(array of an object type); Arrays.sort(array of an object type , from, to); 

but I have no idea about the following two methods.

 Arrays.sort(array of an object type , comparator); Arrays.sort(array of an object type , from, to, comparator); 

Can someone please let me know how to sort an array of an object type using these methods. I ask you to add the code or any link that goes to the .java class. I tried to search, but could not find it.

Thanks.

+2
source share
4 answers

Example:

 class Person{ int id; public getId(){return this.id;} //Other stuff in your custom class } Person[] persons = ...;//An array of person you get from somewhere Arrays.sort(persons,new Comparator<Person>(){ @Override public int compare(Person p1, Person p2){ return p1.getId() - p2.getId(); } } ); 
+3
source

Easy:

The comparator interface gives you control over how you sort your object.

An object can be based on a key that is your wise.

For example, an Account object should be sorted based on AccountNumber

 class Account { String AccountNumber; //Key 1 String AccountName; //Key 2 String GovtID; //Key 3 } 

You can sort any of the three keys.

To have control over sorting, you must define a class that implements the Comparator interface, which defines the logic used for sorting.

 class SortAccountByNumber implements Comparator<Account> { //Implement Unimplemented method @Override public int compare(Account a1, Account a2) { //Read the specification for this method here in the Java Doc. return 0; } } 

Now, to use this, just call

  SortAccountByNumber varSortAccountByNumber = new SortAccountByNumber(); Arrays.sort(arrayOfAccounts,varSortAccountByNumber); 
+1
source

Here is an example where the comparator is not defined on the line.

In any case, this is acceptable, but I think this method is easier to understand.

 class Person { int id; public getId(){ return this.id; } } class PersonComparator implements Comparator<Person> { @Override public int compareTo(Person personOne, Person personTwo) { reuturn personOne.getId() - personTwo.getId(); } } 

Using:

 Person[] personArray = buildArraySomehow(); PersonComparator pc = new PersonComparator(); Arrays.sort(personArray, pc); 

A comparator is an interface with only one method: compareTo.

When you create a Comparator, this is the only method you need to implement.

Note that PersonComparator.compareTo () returns nothing, but returns the difference between the identifiers of two Person objects.

This is due to how the compareTo () method should work:

  • If the first element is "preceded" by the second element, a negative number is returned.
  • If the first element "comes after" the second paragraph, a positive number must be returend.
  • If two elements are equivalent (in terms of ordering), zero should be returned.

Check out the documentation for Comparator for more information ...

0
source

For complex objects, Java does not know how to compare them. So you need to write a Comparator. Usually you select the class element to be compared.

 public class Comp implements Comparator<Test> { @Override public int compare(Test t, Test t1) { return what_you_want_to_compare; } } 
0
source

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


All Articles