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

Thread: Ordering ArrayList by 3 conditions as you add to ArrayList

  1. #1
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Ordering ArrayList by 3 conditions as you add to ArrayList

    Ok, I need to add to an ArrayList in order based on 3 conditions. I need help trying to figure out the process.

    Each object has several variables, but we only care about 3 of them right now. The 3 variables are:
    1) station which is a String.
    2) equipment which is a String.
    3) depart which is a double.

    It must be ordered first by station in alphabetically, second by equipment ordered alphabetically and may contain numbers, and third by depart by largest to smallest.

    I've done this sort of thing before, but only with 1 condition. I figured I would use the String.compareTo(String) method to get my values for station, I figure I can do the same for equipment, and I figure depart can be compared with simple <,>,== statements.

    Can anyone guide me?
    Last edited by aussiemcgr; July 13th, 2010 at 10:45 AM. Reason: Change conditions


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Ordering ArrayList by 3 conditions as you add to ArrayList

    Let me restate the problem to see what you are trying to do.
    You are adding objects to an ArrayList.
    You want to add new objects in station, equipment, dept order so that the list is always ordered.

    Seems you'd need a compare method that compares two objects and tells you if one goes before the other.

  3. #3
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Ordering ArrayList by 3 conditions as you add to ArrayList

    That would actually be the perfect way of doing it. Didnt think that about that...

  4. #4
    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: Ordering ArrayList by 3 conditions as you add to ArrayList

    To pile a bit more onto the plate, you could implement the Comparable or Comparator interface and perform your comparisons there. Doing so would allow you quite a bit more adaptability in that you could do full sorting of a List using the Collections.sort method if that is appropriate, or also allow you to create other types of Collections like a TreeSet or TreeMap which sort the elements upon insertion

  5. #5
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Ordering ArrayList by 3 conditions as you add to ArrayList

    Well, I continued the program without ordering the arraylists and it runs fast enough and correctly so I dont think I'll bother writing in an ordering sequence.

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. [SOLVED] Problem with Ordering an Arraylist of Comparable Objects.
    By Faz in forum Collections and Generics
    Replies: 8
    Last Post: June 16th, 2010, 06:36 PM
  3. ARRAYLIST
    By xs4rdx in forum Collections and Generics
    Replies: 4
    Last Post: March 25th, 2010, 10:36 AM
  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