0

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

4
  • This is C++? Please update your tags. Commented Jan 11, 2015 at 22:56
  • What language is it? You usually use Sort function/method provided by the language or its library, so you must say what technology you use. Commented Jan 11, 2015 at 22:56
  • 2
    @Barmar - looks like java Commented Jan 11, 2015 at 22:56
  • Yeah, probably Java. Commented Jan 11, 2015 at 22:58

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

what if I'm using DefaultListModel ?
0

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.

Comments

0

In Java8 you can sort a list like this:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.