按字母顺序对对象数组进行排序

时间:2013-10-24 08:42:42

标签: java arrays sorting

我有一个对象数组,每个对象都有一个给定的名字和一个姓氏。 使用方法getGivenName和getSurname将这些名称写入对象。

我需要按姓氏的字母顺序对数组中的元素进行排序。 我怎么能这样做?

3 个答案:

答案 0 :(得分:6)

您的比较器

class SampleComparator implements Comparator<YourObject> {
    @Override
    public int compare(YourObject o1, YourObject o2) {

           return o1.getSurname().compareTo(o2.getSurname());
   }
}

您的排序

  Collections.sort(YourList, new SampleComparator())

如果您需要忽略大小写,请使用

  return o1.getSurname().compareToIgnoreCase(o2.getSurname()); 

答案 1 :(得分:0)

使用List而不是使用Array。您的课程需要implements Comparable interface。 请参阅代码,

通过实施Comparable接口

public class Person implements Comparable<Person>{

private String givenName;
private String surname;

public static void main(String[] args) {
    Person person1 = new Person("a","b");
    Person person2 = new Person("c","d");
    Person person3 = new Person("e","f");

    List<Person> personList = new ArrayList<Person>();
    personList.add(person1);
    personList.add(person2);
    personList.add(person3);
    Collections.sort(personList);
    System.out.println(personList);
}

@Override
public String toString() {
    return "WorkSheet [givenName=" + givenName + ", surname=" + surname
            + "]";
}
    public Person() {
        // TODO Auto-generated constructor stub
    }


public Person(String givenName , String surname) {
    this.givenName = givenName;
    this.surname = surname;
}

public String getGivenName() {
    return givenName;
}

public void setGivenName(String givenName) {
    this.givenName = givenName;
}

public String getSurname() {
    return surname;
}

public void setSurname(String surname) {
    this.surname = surname;
}

@Override
public int compareTo(Person o) {
    // TODO Auto-generated method stub
    return o.getSurname().compareTo(this.getSurname());
}
}   

通过实施Comaparator接口

public class Person implements Comparator<Person>{

private String givenName;
private String surname;

public static void main(String[] args) {
    Person person1 = new Person("a","b");
    Person person2 = new Person("c","d");
    Person person3 = new Person("e","f");

    List<Person> personList = new ArrayList<Person>();
    personList.add(person1);
    personList.add(person2);
    personList.add(person3);
    Collections.sort(personList , new Person());
    System.out.println(personList);
}

@Override
public String toString() {
    return "WorkSheet [givenName=" + givenName + ", surname=" + surname
            + "]";
}

public Person() {
    // TODO Auto-generated constructor stub
}

public Person(String givenName , String surname) {
    this.givenName = givenName;
    this.surname = surname;
}

public String getGivenName() {
    return givenName;
}

public void setGivenName(String givenName) {
    this.givenName = givenName;
}

public String getSurname() {
    return surname;
}

public void setSurname(String surname) {
    this.surname = surname;
}

@Override
public int compare(Person o1, Person o2) {
    // TODO Auto-generated method stub
    return o2.getSurname().compareTo(o1.getSurname());
}
}

答案 2 :(得分:0)

在对自然语言文本进行排序时,建议使用CollatorCollationKeys。其他答案提到的String.compareTo方法将根据字符串中每个字符的Unicode值比较字符串,这可能不是您想要/期望的,具体取决于您输入的字符串和区域设置。

请注意,我写了some utility methods来帮助排序自然语言文本。

E.g。你可以试试像:

public class PersonLocalizer implements Localizer<Person> {
    @Override
    public String getDisplayString(Person person, Locale inLocale) {
        return person.getSurname();
    }
} 

[...]

Localizables.sort(new PersonLocalizer (), persons);

的Maven:

<dependency>  
    <groupId>org.softsmithy.lib</groupId>  
    <artifactId>softsmithy-lib-core</artifactId>  
    <version>0.3</version>  
</dependency>