views:

42

answers:

2

Hello. I have a task to play with Java Collections framework. I need to obtain a users list from a database, and store it in a collection. (This is finished and users are stored in a HashSet). Each user is an instance of Person class described with name, surname, birth date, join date, and some other parameters which are not important now. Next I need to store the list in different collections (there's nowhere stated how many) providing functionality to sort them by:
- name only
- name, surname, birthdate
- join date

Ok, so to start with, my Person stores data as Strings only (should I change dates to Date ?). I've started implementing sorting with "by name, surname, birthdate", cause that's what I get after calling sort on list with Strings. Am I right ?

public List createListByName(Set set){
    List ret = new ArrayList<String>();
    String data = "";

    for(Object p: set){
        data = p + "\n";
        ret.add(data);
    }
    Collections.sort(ret);
    return ret;
}

But what with the rest ? Here's my Person :

class Person {

    private String firstname;
    private String lastname;
    )..)


    Person(String name, String surname, (..)){
        firstname = name;
        lastname = surname;
        (..)
    }

    @Override
    public String toString(){
        return firstname + " " + lastname + " " + (..);
    }
}
+2  A: 

I wouldn't convert everything to strings to start with. I would implement Comparator<Person> and then sort a List<Person>:

public List<Person> createListByName(Set<Person> set){
    List<Person> ret = new ArrayList<Person>(set);
    Collections.sort(ret, new NameSurnameBirthComparator());
    return ret;
}

The NameSurnameBirthComparator would implement Comparator<Person> and compare two people by first comparing their first names, then their surnames (if their first names are equal) then their birth dates (if their surnames are equal).

Something like this:

public int compare(Person p1, Person p2) {
    // TODO: Consider null checks, and what to do :)
    int firstNameResult = p1.getFirstName().compareTo(p2.getFirstName());
    if (firstNameResult != 0) {
        return firstNameResult;
    }
    int surnameResult = p1.getSurname().compareTo(p2.getSurname());
    if (surnameResult != 0) {
        return surnameResult;
    }
    return p1.getBirthDate().compareTo(p2.getBirthDate());
}

And yes, I would store the date of birth as a Date - or preferably as a LocalDate from JodaTime, as that's a much nicer library for date and time manipulation :)

Jon Skeet
so I should write multiple comprators on Person for each task ?
owca
@owca Yes. If you want 3 lists (or sets), each sorted differently, you write a comperator for each sorting.
extraneon
@owca: You could either write one comparator for each overall sort order, or one for each property and then a utility class to combine two comparators to create a new one.
Jon Skeet
A: 

so I should write multiple comprators on Person for each task ?

Given this is a homework task, then I would say that is the way you would start to learn about Comparators.

For interest sake only you can do this by creating a couple of resuable Comparators.

You can use the Bean Comparator to sort on individual properties.

Then you can use the Group Comparator to sort on multiple properties.

camickr
yeah, I've never used them so I'm skimming through the docs now. At first glance I was sure that Comparator is an object that implements Comparable :P
owca