Bubblesort of an ArrayList with objects
Hi,
I have an arraylist containing customer objects with the following attributes and methods:
Code :
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:
Code :
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
Re: Bubblesort of an ArrayList with objects
Why don't you just use the Collections class and a Comparator?
Re: Bubblesort of an ArrayList with objects
what's that? Do you know of an example please?
Re: Bubblesort of an ArrayList with objects
Quote:
Originally Posted by
bondage
what's that? Do you know of an example please?
Did you check out the API for Collections?
Collections (Java Platform SE 6)
Re: Bubblesort of an ArrayList with objects
KevinWorkman,
thanks for the link.
I understand that there are two methods that may be relevant in this case, namely sort(List<T> list) and sort(List<T> list, Comparator<? super T> c).
I do not understand how i can use this latter one in ana array list. I have tried to look for some examples bu could not find.
Re: Bubblesort of an ArrayList with objects
What are you confused about? You have an ArrayList of Customers, right? So make a Comparator for Customer (or make the implement Comparable) that sorts them in whatever fashion you want.
Re: Bubblesort of an ArrayList with objects
Thanks,
I tried to implement something like this in the sort class
Code :
public static void SortCustomers(ArrayList<Customer> customerList)
{
Comparator comparator = Collections.sort([B]arg0[/B]);
Collections.sort(customerList,comparator);
....
However, what should i put as an args0 parameter?
thanks in advance
Re: Bubblesort of an ArrayList with objects
Huh? That's not at all how that method works. The sort() method is void, so it doesn't return anything. Did you read the API link I gave you?
Re: Bubblesort of an ArrayList with objects
KevinWorkman,
thanks for your quick replies.
Yes i read it and i went throuh the examples in USING JAVA COLLECTIONS and Lesson: Algorithms (The Java™ Tutorials > Collections) however i am still at a loss.
Initially you said
Quote:
You have an ArrayList of Customers, right? So make a Comparator for Customer (or make the implement Comparable) that sorts them in whatever fashion you want.
What i am not understanding is how i can implement comparable for this arraylist.
thanks
Re: Bubblesort of an ArrayList with objects
Quote:
Originally Posted by
bondage
What i am not understanding is how i can implement comparable for this arraylist.
thanks
The ArrayList doesn't implement Comparable. The Objects being stored in the ArrayList implement Comparable. If you don't want to do that, create a Comparator that does the same thing.
For more info, again see the API:
Comparable (Java Platform SE 6)
Comparator (Java Platform SE 6)