1

我正在尝试按姓氏对数组进行排序,但我在代码中遇到了障碍,我不知道该怎么做,也找不到任何对我有帮助的东西。我的主要问题在于

     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;
    }
}
4

4 回答 4

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);
        }
    } );
}
于 2013-03-26T00:28:59.383 回答
0

你可以试试:

public int compareTo(Person p){
    return lastName.compareToIgnoreCase(p.lastName);
}
于 2013-03-26T00:33:27.537 回答
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]
于 2013-03-26T00:35:38.450 回答
-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");

此行将根据项目名称升序对我的项目列表(项目是模型类)进行排序

于 2013-03-26T01:07:58.253 回答