0

I have this class:

public class Contact {
private  String firstname;
private String lastname;
private List<Integer> phoneNumber;
private Scanner in;
public Contact(){
    phoneNumber = new ArrayList<>();
    firstname = lastname = "";
    in = new Scanner(System.in);
}
public void setFirstName(){
    firstname = in.nextLine();
} 
public  void setLastName(){
    lastname = in.nextLine();
}
public void setPhoneNumber(){
    phoneNumber.add(in.nextInt());
}
public String getFirstName(){
    return firstname;
}
public  String getLastName(){
    return lastname;
}
public  Integer getPhoneNumber(int position){
    return phoneNumber.get(position);
}
}

Now I want to make a class PhoneBook which have my contacts.. I thought to make it with

Arraylist<Contact>

because it won't have a fixed size.. When I want to sort this arraylist by Lastname what would I do?

haffax
  • 5,898
  • 1
  • 32
  • 30
Bbabis
  • 165
  • 1
  • 12
  • This question has been asked numerous times before and could be easily looked on the Internet. http://stackoverflow.com/questions/9679769/sort-an-arraylist-of-objects – Ben Thurley Nov 23 '12 at 14:19

2 Answers2

5

Your Contact class needs to implement the Comparable interface... Then you can use Collections.sort(list) to sort the list.

Edit: If you want to have multiple ways of sorting, then you could also make a class that is implementing the Comparator interface. You can create multiple comparators (or make one configurable), then you can pass the comparator as second parameter to Collections.sort

Here is a link explaining the comparator solution: http://www.vogella.com/blog/2009/08/04/collections-sort-java/

Tom
  • 116
  • 2
  • I can't make the class MyComparable implements Comparator. Can you help me? How I will compare 2 of the lastname of contacts and returned the correct? After: Collections.sort(contacts, new MyComparable()); ? – Bbabis Nov 23 '12 at 14:40
  • Let your MyComparable class implement Comparator instead of Comparator, as you want to compare contact items... – Tom Nov 23 '12 at 15:06
2

You would have to put in a custom comparator on lastname, either as a separate class or an anonymous class:

OK, I'm editing as I have some spare time and I guess you are learning Java :)

Add these two methods to the Contact class to test:

public void setLastName(String lastname) {
    this.lastname = lastname;
}
@Override
public String toString() {
    return getLastName();
}

Test:

public class Sort {

    static List<Contact> list = new ArrayList<Contact>();
    static Contact one = new Contact(); 
    static Contact two = new Contact(); 
    static Contact three = new Contact(); 
    public static void main(String[] args) {
        one.setLastName("Smith");
        two.setLastName("Monks");
        three.setLastName("Aaron");
        list.add(one); list.add(two); list.add(three);
        System.out.println("Before: " + list);
        Collections.sort(list, new Comparator<Contact>() {
            public int compare(Contact contact, Contact another) {
                return contact.getLastName().compareToIgnoreCase(another.getLastName());
            }
        });
        System.out.println("After: " + list);
    }
}

Your result should be:

Before: [Smith, Monks, Aaron]
After: [Aaron, Monks, Smith]
Akber Choudhry
  • 1,755
  • 16
  • 24
  • I want sort my ArrayList contacts = new ArrayList<>(); (Contact is a class with many fields) by the contact's field lastname(String).. So I will write: Collections.sort(contacts, new Comparator??????>) .. Please help me for the code.. I meet this problem many times and I want a solution.. – Bbabis Nov 23 '12 at 14:52