• Status: Solved
• Priority: Medium
• Security: Public
• Views: 664

# Java merging and sort list of objects

I am looking for simple comparator logic or any other solution that helps merge objects within a List itself.

Suppose List represents database table with 2 pairs of columns (so  overall  4 columns with 4 integer values.) So every list object has  two individual pairs (containing two values ).

Initial state of every object:

## 1

Every list object is unique in the list.

## 2

Every list object consist 2 pairs of integer values. Suppose a pair_X and a pair_Y. (overall 4 integer values)

## 3

Every pair has 2 integer values. Suppose x1 and x2 and y1 and y2.

## 4

Value x1 is always less then x2. (x1 < x2)
And Value y1 is always less then y2. (y1 < y2)

## 5

Balance in every pair is always equal. (x2 - x1) == (y1 - y2).

In final state

So reducing of List size through, merging of two (or more list objects). According following criteria:

x2 value (e.g. 10) of 1st object, is next number of x1 value  (e.g. 11)  of 2nd object.

AND

y2 value (e.g. 14) of 1st object, is next number of y1 value (e.g. 15)  of 2nd object.

Is there anybody who is willing to help a newbie? And show how to solve this problem in Java, with concrete programming code / examples?

Pls see, below I added an example and also attached txt data file if necessary for testing.

Thanks.
A Simplified example just for 4 Columns (which are 2 pairs).

Example:

#Column_1  Column_2  Column_3  Column_4,#

1. 506         520      771         785
2. 106         110	    210       	214
3. 502    	   505      181         184
4. 714         717	    270 	273
5. 106         110	    310       	314
6. 111	   115	    215	      	219
7. 521         524      767         770
8. 502   	   505      350   	353
9. 100	   105	    204       	209

-------------------- after merging and sorting-----------------

1. 100	    115	    	204     219
2. 106	    110	    	310	314
3. 502          505     	181   	184
4. 502          520     	767   	785
5. 714          717	    	270 	273
Sampletable-4int-withou-hypen.txt
0
4xaoc
• 6
• 4
• 3
• +1
1 Solution

IT ExpertCommented:
Hi, I do not have the time to understand your issue with seems not trivial.

Perhaps you will find something useful here: http://www.google.com/search?q=java+sort+object
since it combines sorting with object manipulation which is what I think you need, e.g. create a compound object from your columns and sort them while manipulating them.
0

Author Commented:
Hi mplungian, my issue is really trivial. Generally,

1.)  I can sort List_1 asc,
2.) create a second  List_2 and then
3.) by iterating through  List_1,
4.)    looking for adjacent element in L matching this cirteria ->. (obj1.b==obj2.a - 1)  && (obj1.d == obj2.c -  4.1) if there is a match, overwriting in list2 ( obj1.b = obj2.b, obj1.d=obj2.d),
4.2) if no match copy element into List_2

But here is a problem. If in entire  List_1 exits an element that match criteria (obj1.b==obj2.a - 1)  && (obj1.d == obj2.c -1),  but element is not adjacent element of for my current element (suppose acturally holding by iterator). I can't merge them.

I do not know how to transfer this logic into java programming code. That's all!!
Is it really, that much difficult for an "Expert"??
a -  b - c - d

1.)    6    8   16  18
2.)    38  40   55  57
3.)    6    8   25  27
4.)    1    5   11  15
5.)    6    8    3   5
6.)    9   12   19  22
7.)    18  20    1   3
8.)    23  25   15  17

---- Sorted Order ----

4.)    1    5   11  15
5.)    6    8    3   5
1.)    6    8   16  18
3.)    6    8   25  27
6.)    9   12   19  22
7.)    18  20    1   3
8.)    23  25   15  17
2.)    38  40   55  57

------ expected result after merging------

4.)    1    12   11  22
5.)    6    8    3   5
3.)    6    8   25  27
7.)    18  20    1   3
8.)    23  25   15  17
2.)    38  40   55  57
0

Author Commented:
Hi mplungian, my issue is really trivial. Generally,

1.)  I can sort List_1 asc,

2.) create a second  List_2 and then

3.) by iterating through  List_1,

4.)    looking for adjacent element in L matching this cirteria
->. (obj1.b==obj2.a - 1)  && (obj1.d == obj2.c -1)

4.1) if there is a match, overwriting in list2 ( obj1.b = obj2.b, obj1.d=obj2.d),

4.2) if no match add element into List_2 as new element.

But here is a problem.

If in entire  List_1 exits an element that match criteria (obj1.b==obj2.a - 1)  && (obj1.d == obj2.c -1) (line number 1 in example)

but existing element is not adjacent element of my current element (suppose iterator acturally holding them). So I can't merge them.

I do not know how to transfer this logic into java programming code. That's all!!
Is it really, that much difficult for an "Expert"??
0

IT ExpertCommented:
Erm... I am not a JAVA expert and I still after reading through your description several times do not quite see the logic without spending more time than I currently have. I just wanted to help you since no-one seemed to have made any comments.
0

Author Commented:
Anyway, thanks for trying...
0

IT ExpertCommented:
YW. I pushed the "Request Attention" for you
0

Commented:
This class will sort your objects :

import java.util.Comparator;
public class YourClass implements Comparator<YourClass> {
int x1, x2, y1, y2;

public int compare(YourClass a, YourClass b) {
if (a.x1 > b.x1) return 1;
if (a.x1 < b.x1) return -1;
// if we are here it means that a.x1 = b.x1, so we have to compare a.y1 and b.y1
if (a.y1 > b.y1) return 1;
if (a.y1 < b.y1) return -1;
// if we are here it means that a.y1 = b.y1, so a is equal to b
return 0;
}

}
0

Commented:
ok, I'll send you solution tomorrow
0

Commented:
Download the files, attached to this comment. This is what you need.
In your comment #33690322 in section ------ expected result after merging------
the first element shoul be 1    5   11  15, isn't it?
Good luck!
YourClass.java
YourClassComparator.java
0

Author Commented:
Hi Valera!!  First element after merging should be 1    12   11  22. If you keep a closer look on  lines
4.)    1    5   11  15
1.)    6    8   16  18
6.)    9   12   19  22,
you'll see some order:

4.)    1-5   11-15,
1.)    6-8   16-18,
6.)    9-12   19-22,

Step 1:) 1 . .  . 5, 6 . 8, 9 . . 12 and  11 . . . 15, 16 . 18, 19 . . 22.
Step 2:) 1 . .  .  . . . 8, 9 . . 12 and  11 . . . . . . 18, 19 . . 22.
Step 3:) 1 . .  .  . . . . . . . 12 and  11 . . . . . . . . . . 22.

That's all.  some kind of merging that I have to do with a large ArrayList.

Thanks  a lot for programming code example. Actually this gives me a great thought-provoking impulse.
0

Commented:
Ahaaaaaaa! I got the point! Now I understood what merge means.
I was thinking that the equal elements must disapear from the list and that's all, but I was wrong...
Ok, I 'll try to rework the code I sent you.
0

Commented:
This is the updated version of YourClass.java, the other file is the same.
equals() method has small changes.
merge() method has two loops
1. outer loop - from second element to the end of the list
2. inner loop(!) - from the previous element of current one (from outer loop) to the first element of the list - that merges the elements!
Good luck!
YourClass.java
0

Commented:
Hi 4xaoc,
The updated version does exactly what you need. Did you see it?
0

Commented:
This question has been classified as abandoned and is being closed as part of the Cleanup Program. See my comment at the end of the question for more details.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.