ArrayList Sorting Based on Method

72 Views Asked by At

I am in need of some help trying to compare 2 arraylist and then sorting; If I am even on the right track. So here is my problem..

Say arraylist 1 contains the objects in which 3 integers adds up to 4 and each integer has to be at least greater or greater/equal to the next number.

So for example, arraylist 1 contains {(2,1,1), (2,2,0), (3,1,0), (4,0,0)}.

Also each object integers are sorted from greatest to least.

Now I have a take the (2,1,1) and send it to a method to perform an algorithm on it. for each integer place, i need to add 2 to that integer place, and subtract 1 from the rest. We can call these A event, B event, or C Event.

for example, (2,1,1) which these events would be

A Event: (4,0,0)

B Event: (3,1,0)

C Event: (3,1,0)

Now, my question is because A event produced (4,0,0) how would I sort the first arraylist to have that number come next and then have (3,1,0) but without (3,1,0) duplicating. So after sorting the array, it should be

(2,1,1) ( 4,0,0) (3,1,0) (2,2,0) 
1

There are 1 best solutions below

2
D.B. On BEST ANSWER

At present your question is not clear to me, but I think I can help point you in the right direction.

Since you said:

without (3,1,0) duplicating

I'd suggest that to ensure uniqueness of objects in a Collection you should consider using a class that implements the Set interface (see Javadoc for Set for details).

Assuming you are not writing the sorting algorithm yourself you could use a SortedSet and then implement the necessary functionality (Comparable interface or Comparator) so that your objects are sorted in the way you want (the earlier link provides links that describe how to do this).

Hope this helps, and if you could try to make the question more clear I'd be happy to offer some additional pointers.

Here are some of the confusing parts that you could work on:

each integer has to be at least greater or greater/equal to the next number.

This is confusing because you're giving two conflicting requirements. Is each number strictly greater than the next or is it greater than or equal to the next?

Also each object integers are sorted from greatest to least.

I'm not sure what you mean by this because you already said that the integers contained in the objects were in a specific order.

because A event produced (4,0,0) how would I sort the first arraylist to have that number come next

You're not really telling us how the sorting should work. Can you describe the algorithm more - how does it decide the order of the items, how should one item be compared with another?