I have a Customer object class which has some variables and is implementing a Comparator already regarding one of those variables. However I need to implement another Comparator for a different variable last_name.
Since I can't have 2 compareTo() methods in my Customer class I decided to make a Comparing class specifically for this here
public class CompareByLastName implements Comparator<Customer> {
private List<Purchase> purchases;
private List<Customer> customers;
public CompareByLastName(List<Purchase> purchases, List<Customer> customers) {
this.purchases = purchases;
this.customers = customers;
}
/**
* @param descending
* @return will be a sorted, in ascending, or descending, array of customer's according to their authors.
*/
public List<Purchase> sortByLastName(boolean descending){
List<Purchase> return_List = new LinkedList<Purchase>();
Collections.sort(customers);
if(descending == true) {
Collections.reverse(customers);
}
for(Customer customer : customers) {
for(Purchase purchase_info : purchases) {
if(customer.getId() == purchase_info.getCustomer_id()) {
return_List.add(purchase_info);
}
}
}
return return_List;
}
@Override
public int compare(Customer customer_1, Customer customer_2) {
int result = customer_1.getLastName().compareTo(customer_2.getLastName());
if(result < 0) {
return -1;
}
else if(result > 0) {
return 1;
}
else {
return 0;
}
}
}
but once it hits Collections.sort(customers);
it doesn't activate the public int compare(Customer customer_1, Customer customer_2) below.
Frankly I don't know what its using as a comparator in the sort; does anyone know how to fix this problem and get it sorting by the last_name?
Oh and once it gets to return it some how manages to go from 100(0-99) items in purchases to 103(0-102) items in return list? No idea how that's happening.
fixed this part I had switch the for loops reading Purchase then going through the list of all the customers and finding matches instead of vise versa.
Any help is appreciated.
Thanks in advance.