简体   繁体   中英

Sorting a list with objects?

In the contacts class (implements actionListener) I have a JList that contains objects (elements from a file). There are sorting buttons, each button has an 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>

Implement class ContactsRenderer extends JLabel implements ListCellRenderer. See java doc for more details: 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

In java you can sort a list like so:

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. So, to reverse the order, you would use: obj2.getFirstName().compareTo(obj1.getFirstName()); or, you could just multiply by -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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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