I'm an amateur programmer and I've created a program that contains an arraylist that holds different phone book contacts and I'm trying to sort the arraylist lexicographically using a compareTo method. I don't know how to properly call the method in the main method so that it sorts properly and I'm not allowed to use collections sort. Can anyone help me?
Here's some of my code:
public class Phonebook implements Comparable<Phonebook> {
private String first, last;
public Contact(String first, String last) {
this.first = first;
this.last= last;
}
public String getFirst() {return first;}
public String getLast() {return last;}
public String toString() {
return first + " " + last;
}
public int compareTo(Phonebook another) {
int a = last.compareTo(another.last);
int b = first.compareTo(another.first);
if (a== 0 && b== 0)
return 0;
if (a == 0 && b!= 0)
return b;
return a;
}
}
public class PhonebookList implements Iterable<Phonebook>{
ArrayList<Phonebook>phonebook;
public PhonebookList() {
}
public PhonebookList(Contact[]contacts) {
phonebook=new ArrayList<>(Arrays.asList(phonebooks));
}
import java.util.*;
public static void main(String[] args) {
// TODO Auto-generated method stub
PhonebookList list= new PhonebookList();
Phonebook ph1= new Phonebook ("Brandon","Johnson");
Phonebook ph2 = new Phonebook ("Samantha","Joseph");
list.add(ph1);
list.add(ph2);
}