我正在尝试按姓氏排序数组,我在我的代码中遇到了一堵砖墙,我不知道该怎么做,我找不到任何可以帮助我的东西。 我的主要问题是
public void sortByLastName(){
Collections.sort(list);
}
这两段代码分为两个不同的类,是一个问题吗?
public int compareTo(Person p){
int compareResult = this.lastName.compareTo(p.lastName);
if(compareResult == 0){
return 0;
}
else
if(compareResult > 0){
return 1;
}
else
return -1;
}
}
答案 0 :(得分:3)
如果您希望以多种方式对任何事物进行排序,您将很快确定最好的做法是将比较函数作为参数传递给Collections.sort
。
public void sortByLastName(){
Collections.sort(list, new Comparator<Person>() {
public int compare(Person lhs, Person rhs){
return lhs.lastName.compareTo(rhs.lastName);
}
} );
}
答案 1 :(得分:0)
您可以尝试:
public int compareTo(Person p){
return lastName.compareToIgnoreCase(p.lastName);
}
答案 2 :(得分:0)
您的compareTo
方法需要在您的Person
课程中。您的Person
课程需要implement Comparable<Person>
。然后Collections.sort
将有效 -
private static final class Person implements Comparable<Person> {
private String firstName;
private String lastName;
public Person(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public int compareTo(Person o) {
int c = getLastName().compareTo(o.getLastName());
if (c != 0) {
return c;
}
return getFirstName().compareTo(o.getFirstName());
}
@Override
public String toString() {
return getFirstName() + " " + getLastName();
}
}
public static void main(String[] args) {
final List<Person> people = new LinkedList<Person>();
people.add(new Person("John", "Smith"));
people.add(new Person("Hans", "Mustermann"));
people.add(new Person("John", "Doe"));
System.out.println(people);
Collections.sort(people);
System.out.println(people);
}
输出:
[John Smith, Hans Mustermann, John Doe]
[John Doe, Hans Mustermann, John Smith]
答案 3 :(得分:-1)
我已经创建了一个函数来按照模型类中的特定变量对对象列表进行排序。它可能与您需要的不匹配,但也许您可以接受这个想法。我正在使用反射能够使用任何数据类型对任何类模型进行排序。
public Vector sort(Vector list, String kelas, String s, String asc) throws NoSuchMethodException {
try {
// Creates an object of type Class which contains the information of
// the class String
Object[] obj = list.toArray();
Object[] args = {};
Class cl = Class.forName(kelas);
Method toSort = cl.getMethod(s, null);
if (asc.equalsIgnoreCase("desc")) {
if (toSort.getReturnType().toString().equalsIgnoreCase("int") || toSort.getReturnType().toString().equalsIgnoreCase("double")) {
for (int i = 0; i < obj.length; i++) {
for (int j = i; j < obj.length; j++) {
try {
if (Double.parseDouble(toSort.invoke(obj[i], args).toString()) < Double.parseDouble(toSort.invoke(obj[j], args).toString())) {
Object temp = obj[i];
obj[i] = obj[j];
obj[j] = temp;
}
} catch (IllegalAccessException ex) {
Logger.getLogger(DbBean.class.getName()).log(Level.SEVERE, null, ex);
} catch (IllegalArgumentException ex) {
Logger.getLogger(DbBean.class.getName()).log(Level.SEVERE, null, ex);
} catch (InvocationTargetException ex) {
Logger.getLogger(DbBean.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
} else {
for (int i = 0; i < obj.length; i++) {
for (int j = i; j < obj.length; j++) {
try {
if (toSort.invoke(obj[i], args).toString().compareToIgnoreCase(toSort.invoke(obj[j], args).toString()) < 0) {
Object temp = obj[i];
obj[i] = obj[j];
obj[j] = temp;
}
} catch (IllegalAccessException ex) {
Logger.getLogger(DbBean.class.getName()).log(Level.SEVERE, null, ex);
} catch (IllegalArgumentException ex) {
Logger.getLogger(DbBean.class.getName()).log(Level.SEVERE, null, ex);
} catch (InvocationTargetException ex) {
Logger.getLogger(DbBean.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
} else {
if (toSort.getReturnType().toString().equalsIgnoreCase("int") || toSort.getReturnType().toString().equalsIgnoreCase("double")) {
for (int i = 0; i < obj.length; i++) {
for (int j = i; j < obj.length; j++) {
try {
if (Double.parseDouble(toSort.invoke(obj[i], args).toString()) > Double.parseDouble(toSort.invoke(obj[j], args).toString())) {
Object temp = obj[i];
obj[i] = obj[j];
obj[j] = temp;
}
} catch (IllegalAccessException ex) {
Logger.getLogger(DbBean.class.getName()).log(Level.SEVERE, null, ex);
} catch (IllegalArgumentException ex) {
Logger.getLogger(DbBean.class.getName()).log(Level.SEVERE, null, ex);
} catch (InvocationTargetException ex) {
Logger.getLogger(DbBean.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
} else {
for (int i = 0; i < obj.length; i++) {
for (int j = i; j < obj.length; j++) {
try {
if (toSort.invoke(obj[i], args).toString().compareToIgnoreCase(toSort.invoke(obj[j], args).toString()) > 0) {
Object temp = obj[i];
obj[i] = obj[j];
obj[j] = temp;
}
} catch (IllegalAccessException ex) {
Logger.getLogger(DbBean.class.getName()).log(Level.SEVERE, null, ex);
} catch (IllegalArgumentException ex) {
Logger.getLogger(DbBean.class.getName()).log(Level.SEVERE, null, ex);
} catch (InvocationTargetException ex) {
Logger.getLogger(DbBean.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
}
list = new Vector();
for (int i = 0; i < obj.length; i++) {
list.add(obj[i]);
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return list;
}
你可以像这样简单地调用该函数:
Vector sortedList=sort(UnsortedList, "bean.Items", "getItemName", "asc");
此行将根据项目名称升序
对项目列表(项目是模型类)进行排序