java 使用 Arrays.sort() 方法对类型对象的数组进行排序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14863433/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 17:39:57  来源:igfitidea点击:

sorting array of type object using Arrays.sort() method

javacomparisonsorting

提问by nakul

I know how to sort an array of objects using Arrays.sort() method in following way.

我知道如何使用 Arrays.sort() 方法按以下方式对对象数组进行排序。

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 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 array of type object using these methods.I request you to add code or any link that directs to the .java class.I tried to search it but could not find it.

有人可以让我知道如何使用这些方法对类型对象的数组进行排序。我请求您添加代码或任何指向 .java 类的链接。我试图搜索它但找不到它。

Thanks.

谢谢。

回答by Cratylus

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();  
   }  
} ); 

回答by user1428716

Its easy:

这简单:

Comparator Interface gives you control over the way you sort your Object.

比较器接口使您可以控制对对象进行排序的方式。

An Object can be based on a key which is your wise.

一个对象可以基于一个你明智的键。

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

例如,应根据 AccountNumber 对 Account 对象进行排序

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

You can sort on either of three keys.

您可以对三个键中的任何一个进行排序。

In order to have control on the sorting, you have to define a class that implements the Comparator interface, which will define the logic used for sorting.

为了控制排序,您必须定义一个实现 Comparator 接口的类,该类将定义用于排序的逻辑。

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, simply call

现在要使用它,只需调用

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

回答by jahroy

Here's an example where the comparator is not defined inline.

这是一个比较器未内联定义的示例。

Either way is acceptable, but I think this way 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();
    }
}

Usage:

用法:

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

Comparator is an interface with only one method: compareTo.

Comparator 是一个只有一个方法的接口:compareTo。

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

创建 Comparator 时,这是您需要实现的唯一方法。

Note that PersonComparator.compareTo()does nothing but return the difference of the IDs of the two Person objects.

请注意,PersonComparator.compareTo()仅返回两个 Person 对象的 ID 差值。

This is because of how the compareTo()method is supposed to work:

这是因为compareTo()方法应该如何工作:

  • If the first item "comes before" the second item, a negative number should be returned.
  • If the first item "comes after" the second item, a positive number should be returend.
  • If the two items are equivalent (in terms of ordering), zero should be returned.
  • 如果第一项“出现在”第二项之前,则应返回负数。
  • 如果第一项“”第二项之后,则应返回正数。
  • 如果这两个项目是等价的(在排序方面),则应返回零。

Check out the documentation for Comparatorfor more info...

查看Comparator的文档以获取更多信息...

回答by Simon Dirmeier

For complex objects Java does not know how to compare them. Thus you need to write an Comparator. Normally you choose a member of the class that has to be compared.

对于复杂的对象,Java 不知道如何比较它们。因此,您需要编写一个比较器。通常,您选择必须进行比较的类的成员。

public class Comp implements Comparator<Test> {

    @Override
    public int compare(Test t, Test t1) {
       return what_you_want_to_compare;
    }    
}