0

So I am to create this program that creates an array of persons which is sorted by a method in the class Algorithms. I am to create the interface Sortable which defines a comparison method called compareTo which should compare 2 objects to see which comes first. The Person Class represents a person and implements Sortable, and the Algorithms class has a method named sort which takes an array consisting of Sortable objects (Persons) and sort these. I am stuck, and my coursebook is not helping me much here.

public interface Sortable <T> {
int compareTo(T ob);
}

.

public class Algorithms implements Sortable <Person>{

    public int compare(Person p1, Person p2){
      return p1.lastName().compareTo(p2.lastName());
  }
}

.

public class Person implements Sortable<Person>
{
String firstName;
String lastName;
String dob;

public Person (String lastName, String firstName, String dob){
this.lastName=lastName;
this.firstName=firstName;
this.dob=dob;
}

public String lastName(){
return lastName;
}

public String firstName(){
return firstName;
}
public String dob(){
return dob;
}

@Override
public int compareTo(Person o){
    Person p = (Person)o;
    int last = lastName.compareTo(o.lastName);
        return last;
}

public String toString(){
return "Namn "+ lastName +" "+ firstName +" Personnummer: "+dob;
}
}

.

public class Personer {
public static void main(String[]args){
    Person p1 = new Person ("Ek","Ida","530525-0055") ;
    Person p2 = new Person ("Björk","Sten","650203-0250");
    Person p3 = new Person ("Al", "Bengt","881212-4455");

    List <Person> list = new ArrayList<>();
    list.add (p1);
    list.add (p2);
    list.add (p3);

    Arrays.sort(list, new Algorithms());

    System.out.println("lista: "+list);

}
}

The question is really what do I need to do to make this code do what I want it to do, which in the end is to print out the names and dob of a number of people in alphabetical order based om last name

2
  • did you try to debug ? Commented Apr 11, 2016 at 10:56
  • I think you should be implementing Comparable. Commented Apr 11, 2016 at 10:56

3 Answers 3

1

You Shall implement Comparable on your Person Class:

public class Person implements Comparable<Person> {
    String firstName;
    String lastName;
    String dob;

    public Person(String lastName, String firstName, String dob) {
        this.lastName = lastName;
        this.firstName = firstName;
        this.dob = dob;
    }

    public String lastName() {
        return lastName;
    }

    public String firstName() {
        return firstName;
    }

    public String dob() {
        return dob;
    }

    @Override
    public int compareTo(Person o) {
        int last = lastName.compareTo(o.lastName);
        return last;
    }

    public String toString() {
        return "Namn " + lastName + " " + firstName + " Personnummer: " + dob;
    }
}

And use Collections#sort method to sort list

public class Personer {
    public static void main(String[] args) {
        Person p1 = new Person("Ek", "Ida", "530525-0055");
        Person p2 = new Person("Björk", "Sten", "650203-0250");
        Person p3 = new Person("Al", "Bengt", "881212-4455");

        List<Person> list = new ArrayList<>();
        list.add(p1);
        list.add(p2);
        list.add(p3);

        Collections.sort(list);

        System.out.println("lista: " + list);

    }
}

These two classes are only required.

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

Comments

0

Modify Algorithms class and implement Comparator interface

    import java.util.Comparator;
    public class Algorithms implements Comparator <Person>{

        public int compare(Person p1, Person p2){
          return p1.lastName().compareTo(p2.lastName());
      }
    }

For sorting (in Personer class) use

List <Person> list = new ArrayList<>();
list.add (p1);
list.add (p2);
list.add (p3);
list.sort(new Algorithms());

Comments

0

You can use the sort method available in Collections. Use an anonymous inner class to create a new Comparator which compares on the lastName if you don't want to limit the compareTo in your Person class to only compare on lastName.

    Collections.sort(list, new Comparator<Person>() {

        @Override
        public int compare(Person o1, Person o2) {
            return o1.lastName().compareTo(o2.lastName());
        }
    });

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.