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 3 of 3

Thread: Sorting ArrayList

  1. #1
    Junior Member
    Join Date
    Dec 2010
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Sorting ArrayList

    Hello everyone,


    Let me describe my program a bit.


    There are 5 classes which describe Person, Tool, Building, Room and Organisation. They all have a String field so they are all similar. So I made an Abstract class which includes the getName method (which returns the name of the class)


    The problem starts in this section. I have a class which creates an ArrayList for each and every class like this:

    public class Lists implements Comparable<AllObjects> {
    	private ArrayList<Person> personList;
    	private ArrayList<Device> deviceList;
    	private ArrayList<Building> buildingList;
    	private ArrayList<Room> roomList;
    	private ArrayList<Organisation> orgList;
     
    	public Lists () {
    		this.personList = new ArrayList<Person>();
    		this.deviceList = new ArrayList<Device>();
    		this.buildingList = new ArrayList<Building>();
    		this.roomList = new ArrayList<Room>();
    		this.orgList = new ArrayList<Organisation>();
    	}

    I needed to implement comparable, because I have to sort these ArrayLists according to their Objects' names.

    But it seems that I can't write this comparison in Lists class. So I tried writing this comparable method in the ABSTRACT class of all my objects.

    But I just can't get it working. Can someone please give me a hint ?


    Here is the Person class:
    public class Person extends AllObjects {
    	private String name;
     
    	public Person (String s) {
    		this.name = s;
    	}
    	public String getName() {
        	return this.name;
        }
    }

    And the abstract class of all 5 Classes;

    public abstract class AllObjects implements Comparable<AllObjects>{
    	private String name;
     
     
    	public String getName() {
        	return this.name;
        }
     
    	public int CompareTo(Object tmp1)
        {
           tmp1 = (AllObjects) tmp1;
           int result = this.getName().compareTo(((AllObjects) tmp1).getName());
     
           return result;
        }
    }


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Sorting ArrayList

    Define "But I just can't get it working"...does it compile? Does it not function correctly?

  3. #3
    Junior Member
    Join Date
    Dec 2010
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Sorting ArrayList

    Ok it was my first time sorting an ArrayList and i figured it out,

    Seems like it was a basic error.



    I was using COMPARATOR instead of COMPARABLE and i found the compareTo method overriding a bit hard to write. (Because its been just a few months since i started java..)


    But try-and-learn is the best way possible. Now I figured it all out, but thanks for your concern anyway

Similar Threads

  1. How to use an ArrayList and what is its advantage over array?
    By JavaPF in forum Java SE API Tutorials
    Replies: 4
    Last Post: December 21st, 2011, 04:44 AM
  2. Sorting a Arraylist
    By waterjames in forum Java Theory & Questions
    Replies: 1
    Last Post: November 26th, 2010, 02:47 PM
  3. Ordering ArrayList by 3 conditions as you add to ArrayList
    By aussiemcgr in forum Collections and Generics
    Replies: 4
    Last Post: July 13th, 2010, 02:08 PM
  4. Arraylist or Arraylist Object?
    By igniteflow in forum Collections and Generics
    Replies: 2
    Last Post: September 11th, 2009, 02:08 AM
  5. [SOLVED] Extracting an How to ArrayList from an ArrayList and convert to int??
    By igniteflow in forum Collections and Generics
    Replies: 2
    Last Post: August 16th, 2009, 01:11 PM