我有一个对象数组,每个对象都有一个给定的名称和姓氏。使用方法 getGivenName 和 getSurname 将这些名称写入对象。
我需要按姓氏的字母顺序对数组中的元素进行排序。我怎样才能做到这一点?
你的比较器
class SampleComparator implements Comparator<YourObject> {
@Override
public int compare(YourObject o1, YourObject o2) {
return o1.getSurname().compareTo(o2.getSurname());
}
}
你的排序
Collections.sort(YourList, new SampleComparator())
如果您需要忽略大小写,请使用 like
return o1.getSurname().compareToIgnoreCase(o2.getSurname());
在对自然语言文本进行排序时,建议使用Collator和CollationKeys。其他答案提到的String.compareTo方法将根据字符串中每个字符的 Unicode 值比较字符串,这可能不是您想要/期望的,具体取决于您的输入字符串和语言环境。
请注意,我编写了一些实用方法来帮助对自然语言文本进行排序。
例如,您可以尝试以下操作:
public class PersonLocalizer implements Localizer<Person> {
@Override
public String getDisplayString(Person person, Locale inLocale) {
return person.getSurname();
}
}
[...]
Localizables.sort(new PersonLocalizer (), persons);
马文:
<dependency>
<groupId>org.softsmithy.lib</groupId>
<artifactId>softsmithy-lib-core</artifactId>
<version>0.3</version>
</dependency>
使用List
而不是使用数组。你的班级需要implements Comparable interface
。请看代码,
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());
}
}
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());
}
}