Hi,
I have an arraylist containing customer objects with the following attributes and methods:
import java.io.Serializable; public class Customer implements Serializable{ private int custumerNum; private String id; private String name; private String surname; private int age; private String address; private int mobileNum; //constructor public Customer(int custNum,String id,String name, String surname, int age, String address,int mobileNum) { this.custNum=custNum; this.id=id; this.name=name; this.surname=surname; this.address=address; } public int getCustNum() { return custNum; } public String getId() { return id; } public String getName() { return name; } public String getSurname() { return surname; } public String getAddress() { return address; } //setters public void setCustNum(int newCustNum) { custNum=newCustNum; } public void setId(String newId) { id=newId; } public void setName(String newName) { name=newName; } public void setSurname(String newSurname) { surname=newSurname; } public void setAddress(String newAddress) { address=newAddress; } }
I would like to use a bubble sort, or some sort of algorithm to soft these client objects in the arrayList as outnlined below:
import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; public class sort{ public static void bubbleSortCustomers(ArrayList<Customer> customerList) { // int n = customerList.size(); // for (int pass=1; pass < n; pass++) { // count how many times // // This next loop becomes shorter and shorter // // for (int i=0; i < n-pass; i++) { // if (x[i] > x[i+1]) { // // exchange elements // int temp = x[i]; // x[i] = x[i+1]; // x[i+1] = temp; // } // } // } } }
The algorithms i found (commented in the bubbleSortCustomers class) that i found relate mostly to arrays of integers. However these are not working since i would like to sort the customers arraylist object. Is there a way to implement this?
thanks