Hey guys,
I need to sort an ArrayList but the problem is, even though each class is Extending the class that the ArrayList holds, it can be sorted.
My Sorting Class
public class Sort implements Comparator<Entity>{
public int compare(Entity e1, Entity e2) {
if(e1.getX() > e2.getX()&&e1.getY() > e2.getY()){
return 1;
} else {
return -1;
}
}
public int compare(Block e1, Block e2) {
if(e1.getX() > e2.getX()&&e1.getY() > e2.getY()){
return 1;
} else {
return -1;
}
}
public int compare(Block e1, Player e2) {
if(e1.getX() > e2.getX()&&e1.getY() > e2.getY()){
return 1;
} else {
return -1;
}
}
}
Used:
Collections.sort(ents,new Sort());
my Array List:
public static ArrayList<Entity>ents=new ArrayList<Entity>();
Entity Class(none are actually in array, other classes extend this class):
package com.iso.ent;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
public class Entity {
public float x, y, z;
Sprite sprite;
public float cartx;
public float carty;
public Entity(float cartx, float carty, float z) {
this.z = z;
this.cartx=cartx*40/2;
this.carty=carty*40/2;
x = (cartx - carty) * 40 / 2;
y = (((carty + cartx) / 2) + z) * 40 / 2;
sprite = new Sprite();
}
public Entity(float cartx, float carty, float z, Sprite sprite) {
this.z = z;
x = (cartx - carty) * 40 / 2;
y = (((carty + cartx) / 2) + z) * 40 / 2;
this.sprite = sprite;
}
public void draw(SpriteBatch batch){}
public float getX() {
return x;
}
public void setX(float x) {
this.x = x;
}
public float getY() {
return y;
}
public void setY(float y) {
this.y = y;
}
public float getZ() {
return z;
}
public void setZ(float z) {
this.z = z;
}
public Sprite getSprite() {
return sprite;
}
public void setSprite(Sprite sprite) {
this.sprite = sprite;
}
public void setSprite(Texture tex) {
this.sprite = new Sprite(tex);
}
}
Any help would be great