简体   繁体   English

用对象排序列表?

[英]Sorting a list with objects?

In the contacts class (implements actionListener) I have a JList that contains objects (elements from a file). 在contacts类(实现actionListener)中,我有一个JList,其中包含对象(文件中的元素)。 There are sorting buttons, each button has an actionListener 有排序按钮,每个按钮都有一个actionListener
by firstname 按名字
by last name 按姓氏
by city 按城市
The sorting is done by the elements of objects. 排序是通过对象的元素完成的。

the user should give the first and last name and city for a contact. 用户应提供联系人的名字和姓氏以及城市。 How to do the sorting ?? 怎么做排序?

And to put those into the list I used this code: 并将这些放入列表中,我使用了以下代码:

Map<String, contacts> ma = readFromFile();
for (Map.Entry<String, contacts> entry : ma.entrySet()) {
    contacts c = (contacts) entry.getValue();
    d.addElement(c.getLastName() + "" + c.getFirstName() + "" + c.getCity());
}

How to do the sorting ?? 怎么做排序? plz help 请帮助

Instead of JList<String> , create a JList<contacts> 代替JList<String> ,创建一个JList<contacts>

Implement class ContactsRenderer extends JLabel implements ListCellRenderer. 实现类ContactsRenderer扩展JLabel实现ListCellRenderer。 See java doc for more details: http://docs.oracle.com/javase/7/docs/api/javax/swing/JList.html 有关更多详细信息,请参见Java文档: http : //docs.oracle.com/javase/7/docs/api/javax/swing/JList.html

Implement your custom list data model: 实现您的自定义列表数据模型:

public class ContactsModel extends AbstractListModel<contacts>() {

    private final List<contacts> backingList = ArrayList<contacts>;

    public ContactsModel(Map<String, contacts> ma) {
        //populate backing list here from your map
    }

    public int getSize() { 
        return backingList.size(); 
    }

    public contacts getElementAt(int index) { 
        return backingList.get(index); 
    }
};

Add a sort method to your model: 向模型添加排序方法:

public void sort(Comparator<contacts> comparator) {
    Collections.sort(backingList, comparator);
    fireContentsChanged(this, 0, backingList.size());
}

Implement comparators for every sort use case: 为每种用例实现比较器:

public class LastNameAscendingComparator implements Comparator<contacts> {

    @Override
    public int compare(contacts c1, contacts c2){
        return c1.getLastName().compareTo(c2.getLastName());
    }
}

Finally call your model.sort() with a corresponding comparator 最后,使用相应的比较器调用model.sort()

In java you can sort a list like so: 在Java中,您可以像这样对列表进行排序:

Collections.sort(yourListOfContacts, new Comparator<Contacts>(){

    @Override
    public int compare(Contacts obj1, Contacts obj2){
       obj1.getFirstName().compareTo(obj2.getFirstName());
    }

});

Notice that the return type of the compare function is an integer. 请注意,比较函数的返回类型是整数。 This integer tells the sorting algorithm which one of the two should come first. 该整数告诉排序算法应该首先使用两者中的哪一个。 Note that a -1 means the first element is "less" than the second, a 0 means that they are the same, and a 1 means that the first element is "greater" than the second. 请注意, -1表示第一个元素“小于”第二个元素, 0表示它们相同,而1表示第一个元素“大于”第二个元素。 So, to reverse the order, you would use: obj2.getFirstName().compareTo(obj1.getFirstName()); 因此,要颠倒顺序,可以使用: obj2.getFirstName().compareTo(obj1.getFirstName()); or, you could just multiply by -1 . 或者,您可以乘以-1

The other fields you want to sort by will follow the same sort of pattern, this is just an example. 您要排序的其他字段将遵循相同的模式,这只是一个示例。

In Java8 you can sort a list like this: 在Java8中,您可以像这样对列表进行排序:

Collections.sort(contactList, (contact1, contact2) 
    -> contact1.getName().compareTo(contact2.getName()));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM