Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: Bubblesort of an ArrayList with objects

  1. #1
    Junior Member
    Join Date
    Mar 2010
    Posts
    28
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Bubblesort of an ArrayList with objects

    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


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Bubblesort of an ArrayList with objects

    Why don't you just use the Collections class and a Comparator?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Mar 2010
    Posts
    28
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Bubblesort of an ArrayList with objects

    what's that? Do you know of an example please?

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Bubblesort of an ArrayList with objects

    Quote Originally Posted by bondage View Post
    what's that? Do you know of an example please?
    Did you check out the API for Collections?

    Collections (Java Platform SE 6)
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Junior Member
    Join Date
    Mar 2010
    Posts
    28
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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.

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default 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.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    Junior Member
    Join Date
    Mar 2010
    Posts
    28
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Bubblesort of an ArrayList with objects

    Thanks,

    I tried to implement something like this in the sort class

    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

  8. #8
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default 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?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  9. #9
    Junior Member
    Join Date
    Mar 2010
    Posts
    28
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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

    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

  10. #10
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Bubblesort of an ArrayList with objects

    Quote Originally Posted by bondage View Post
    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)
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Array List; How to pass objects into another arraylist
    By Melvrick in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 24th, 2011, 05:55 AM
  2. Really need some help with Objects inside an ArrayList
    By ISuckSoBad in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 15th, 2011, 12:33 PM
  3. Minimum value of objects in ArrayList
    By Sputnik in forum Loops & Control Statements
    Replies: 2
    Last Post: October 23rd, 2010, 01:20 PM
  4. looping through an ArrayList of objects with their own attributes
    By etidd in forum Java Theory & Questions
    Replies: 2
    Last Post: April 2nd, 2010, 06:15 AM
  5. Arraylist Objects to Database
    By frankycool in forum JDBC & Databases
    Replies: 3
    Last Post: November 15th, 2009, 08:01 PM