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