Handling Sprite Overlapping

Ok so this is the first time I am making a java game so I am fairly new. The game I am working on now is an overhead game. Anyways I have a visual issues with the sprites where they overlap. I mean its normal to have sprites overlap but I do not want one to appear like it is walking on top of the other rather than the ground. The below is an image illustration the issue I want to address. (I am really good at drawing)

On the right you see what I want my sprites to do, but on left you see overlap in the way I am trying to avoid.

Now my approach to this problem is taking my array of monsters (a class that extends JLabels) and reordering them based on which monster is higher I make this occur at every run of my thread. It seems to me that if a monster is higher it should be added to the screen before the one that is lower. So I try to keep resorting the array of monsters at every iteration of my main game loop based on who is higher to account for the overlap


				if (e.getSource() == follower) 
					{
						c =0;
						for (monster m: monsters) 
							{
								//m.isTouching(monsters);
								
								m.movingToPlayer(joe);//1
								Arrays.sort(monsters, new whoHigherComparator());//2
								layeredScene1.add(m, new Integer(3));//3
								monsterBumpers[c].attachTo(m, "monster");//4
								monsterBumpers[c].handleBumps('m',m,monsterBumpers, monsters);//5
								if (m.left == true) m.setIcon(m.animLArr[monAnimCount]);//6
								else m.setIcon(m.animRArr[monAnimCount]);//7
								//m.movingToPlayer(joe);
								c++;//8
								
							}
						
						monAnimCount++;
						if (monAnimCount == 20) monAnimCount = 0; 
					}

The above is code that shows the majority of my games flow I will describe my flow with references to the commented lines above with [num] for better understanding of where I am coming from. Basically, there is a set number of monsters, the monster all locate and move to the player [1], then I attempt to sort the monster array based on height [2], then add my re-sorted monsters back onto the pane in order [3], then bumpers (a type that help me handle collision and make sure monsters don’t overlap too much) attach the current monster [4], if bumpers are touching monsters arae moved away from each other until their bumpers are no longer touching [5], if the monster is moving leftwards set the animation to a leftwards walk [6], and likewise for rightwards walking [7]

By the way this is my comparator


public class whoHigherComparator implements Comparator<monster>
{

	@Override
	public int compare(monster m1, monster m2) {
		System.out.println("comping?");
		int h1 = m1.getY();
		int h2 = m2.getY();
		if (h1  < h2) 
			{
				System.out.println("sss?  " + h1 + "  vs  " +h2 );
				return 1;
			}
		
		else if (h2 > h1) 
			{
				System.out.println("sss$");
				return -1;
			}
		
		return 0;
	}
	
}

At this point everything in my game flow works just fine, its just the annoying “walking on top of each other” effect that I am having trouble handling. And my attempt at this is line [2] & [3] from my game flow which seems like it would be an effective way to handle the “walking on top of another” effect but its surely not working. I was wonder if you guys have any idea what I can do to tweak my code to effectively handle this situation. Thank you for reading through, and any help is greatly appreciated. Cheers

If I am understanding you right you are doing it backwards. On the picture on the right it seems like the one on top has a greater Y value. So just flip the return values in the comparator.

Wouldn’t you want to sort the monsters before looping through them? (or after they all move around, but before rendering)
Also, no need to create a new comparator each time, just have a static instance.

Also you can simplify the comparison via Integer.compare(i1, i2)

And, yeah, you might just be sorting it in reverse order.


				if (e.getSource() == follower) 
					{
						c =0;
						for (monster m: monsters) 
							{
								//Arrays.sort(monsters, new whoHigherComparator()); //I tried having the sort happen here
								
								m.movingToPlayer(joe);
								
								layeredScene1.add(m, new Integer(3));
								monsterBumpers[c].attachTo(m, "monster");
								monsterBumpers[c].handleBumps('m',m,monsterBumpers, monsters);
								if (m.left == true) m.setIcon(m.animLArr[monAnimCount]);
								else m.setIcon(m.animRArr[monAnimCount]);
								//m.movingToPlayer(joe);
								c++;
								
								Arrays.sort(monsters, new whoHigherComparator()); //I tried having the sort happen here too
								
							}
						
						monAnimCount++;
						if (monAnimCount == 20) monAnimCount = 0; 
					}

I also swapped the returns from my comparator but still no changes. It seems that maybe the array isn’t being sorted and/or these changes are just not being reflected. Any ideas guys? Thanks again!