Sort a complex ArrayList with a lot of data
in
Programming Questions
•
11 months ago
Hello all,
new day, new problem !!!
I try to make a layer system and sort the layer from the top to the bottom.
But I've big problem to sort the objects from my ArrayList.
The idea it's to show the level of the layer and give the position, if it's the first, second,... or the last.
I try to extract the level of my layer list to copy in list of rank to sort this one, because I don't manage to sort directly my list of object... I try to make an example short, but don't manage too. So I give the big sketch.
I see few solution in the forum, but very complex for me...I just find the solutions with Collections.sort(arg).
I show the problem is in the part 3 between ///////////////////// and ///////////////
new day, new problem !!!
I try to make a layer system and sort the layer from the top to the bottom.
But I've big problem to sort the objects from my ArrayList.
The idea it's to show the level of the layer and give the position, if it's the first, second,... or the last.
I try to extract the level of my layer list to copy in list of rank to sort this one, because I don't manage to sort directly my list of object... I try to make an example short, but don't manage too. So I give the big sketch.
I see few solution in the forum, but very complex for me...I just find the solutions with Collections.sort(arg).
I show the problem is in the part 3 between ///////////////////// and ///////////////
- color BG = color(100,95,95) ;
ControlLayer cLayer ;
PVector posLayer = new PVector (0,0) ;
PVector sizeLayer = new PVector (0,0) ;
float rowHeight = 30.0 ;
float margin = 6.0 ;
void setup ()
{
size (500, 500 ) ; background( BG) ;
cLayer = new ControlLayer(rowHeight) ;
}
void draw ()
{
background( BG) ;
cLayer.display() ;
float mrg = margin /2.0 ;
for (int i = 0 ; i < height ; i += rowHeight ) {
stroke(0) ;
strokeWeight(1) ;
line (0, i-mrg , width , i-mrg ) ;
line (0, i+mrg , width , i+mrg ) ;
}
}
void mousePressed()
{
float posY = (floor(mouseY / rowHeight) * rowHeight) + rowHeight / 2.0 ;
// println (posY) ;
posLayer.x = mouseX ;
posLayer.y = posY ;
sizeLayer.x = width ;
sizeLayer.y = rowHeight -margin ;
int identity = round(random(1,6)) ;
cLayer.addLayer(posLayer, sizeLayer, identity) ;
} - class ControlLayer
{
ArrayList<Layer> listLayer ;
float rowHeight ;
int maxLayer = 5 ;
ControlLayer(float rowHeight)
{
this.rowHeight = rowHeight ;
listLayer = new ArrayList<Layer>() ;
}
//////////////////
void addLayer (PVector p, PVector s, int ID)
{
for ( Layer lyr : listLayer ) {
if (lyr.detection() || listLayer.size() > maxLayer-1) {;
return ;
}
}
//Layer lyr = new Layer(p, s, rowHeight, Layer.ID1) ; // example to call the ID directly from the class Layer
Layer lyr = new Layer(p, s, rowHeight, ID) ;
listLayer.add(lyr) ;
}
void display()
{
for ( Layer lyr : listLayer ) {
lyr.drag(listLayer) ;
lyr.update() ;
lyr.collision(listLayer) ;
println(lyr.getRanking() ) ;
lyr.display() ;
lyr.info(listLayer) ;
}
}
} - class Layer
{
private color c ;
private color noir = color (0) ;
private color blanc = color (255) ;
private color rouge = color (215,30,30 ) ;
private color orange = color (255,150,0 ) ;
private color jaune = color (255,230,0 ) ;
private color violet = color (150,90,200 ) ;
private color vert = color (160,180,70 ) ;
private color bleu = color (90,170,200 ) ;
private color IDcolor1 = orange ;
private color IDcolor2 = jaune ;
private color IDcolor3 = rouge ;
private color IDcolor4 = violet ;
private color IDcolor5 = bleu ;
private color IDcolor6 = vert ;
PVector p, s ;
float newPosX, newPosY ; // we cannot use PVector for this relay, if we use PVector relay the object don't come back
float rowHeight ;
// identity of layer
int ID = 0 ;
final static int ID1 = 1 ;
final static int ID2 = 2 ;
final static int ID3 = 3 ;
final static int ID4 = 4 ;
final static int ID5 = 5 ;
final static int ID6 = 6 ;
// ranking of layer
int rankLayer = 1 ;
ArrayList listRanking ;
// boolean to move the layer
boolean inside, locked, contact ;
boolean dropped = true ;
Layer(PVector p, PVector s, float rowHeight, int ID)
{
rectMode (CENTER) ;
this.p = p ;
newPosX = p.x ;
newPosY = p.y ;
this.s = s ;
this.rowHeight = rowHeight ;
this.ID = ID ;
listRanking = new ArrayList() ;
}
void update()
{
float posY = (floor(mouseY / rowHeight) * rowHeight) + rowHeight / 2.0 ; // move in the row grid
if(locked) { // if the layer is locked he follow the mouse
newPosX = mouseX ;
newPosY = posY ;
} else if (contact && !locked && !dropped) {
c= blanc ;
newPosX = p.x ;
newPosY = p.y ;
} else if (contact) {
c= blanc ;
} else {
c = noir ;
newPosX = newPosX ;
newPosY = newPosY ;
}
}
void display()
{
if ( ID == 1 ) c = IDcolor1 ;
else if ( ID == 2 ) c = IDcolor2 ;
else if ( ID == 3 ) c = IDcolor3 ;
else if ( ID == 4 ) c = IDcolor4 ;
else if ( ID == 5 ) c = IDcolor5 ;
else if ( ID == 6 ) c = IDcolor6 ;
else c = noir ;
fill(c) ;
noStroke() ;
// rect (newPosX, newPosY, s.x, s.y ) ; // basic
rect (width/2, newPosY, s.x, s.y ) ;
}
// DRAG
void drag(ArrayList<Layer> listLayer)
{
// check if boolean inside is true and the mouse is pressed
if (detection() ) inside = true; else inside = false;
if ( inside && mousePressed && !otherLocked(listLayer) ) { locked = true ; dropped = false ; }
if ( !mousePressed ) { locked = false ; }
if ( !mousePressed && !inside ) { dropped = true ; }
}
//COLLISION.CONTACT.NoCONTACT
//::Collision
void collision(ArrayList<Layer> listLayer )
{
for (Layer target : listLayer) {
if (target != this) {
//check if two layers are in contact
if (collide(target) ) {
contact = true ;
break ; // we must stop the loop at the first collide
} else {
contact = false ;
}
}
}
}
//INFO
//show info
void info (ArrayList<Layer> listLayer)
{
fill (noir) ;
rankingLayers(listLayer) ;
text (rank() + " / " + rankLayer, 10, newPosY +5 ) ; // give the rank of layer from the top of board
}
///////////////THE PROBLEM IS HERE////////////////////////
/////////////////////////////////////////////////////////
// ranking the layer from the top to the bottom
void rankingLayers(ArrayList<Layer> listLayer )
{
// clear the rank list to update the ranking
listRanking.clear() ;
for (Layer target : listLayer) {
if ( listRanking.size() != listLayer.size() ) {
listRanking.add(target.rank()) ;
}
}
Collections.sort(listRanking) ;
// String r = (listRanking) ;
// println(listRanking) ;
/*
listLayer.clear() ;
for( Layer lyr : listRanking ) {
if ( listRanking.size() != listLayer.size() ) {
listLayer.add(lyr) ;
}
}
*/
// println(listRanking) ;
}
//RETURN
// return rank from the top of board
ArrayList getRanking() {
return listRanking ;
}
////////////////////////////////////////////////////////
////////////////////////////////////////////////////////
int rank() {
int r = floor(newPosY / rowHeight) +1 ;
return r ;
}
// check if the cursor is on the layer or not
boolean detection() {
if( mouseX > newPosX - s.x /2.0 &&
mouseX < newPosX + s.x /2.0 &&
mouseY > newPosY - s.y /2.0 &&
mouseY < newPosY + s.y /2.0) {
return true;
} else {
return false;
}
}
//
// Make sure no other buttons are active in the list of objects wished
boolean otherLocked(ArrayList<Layer> listLayer)
{
for (Layer lyr : listLayer ) { // don't need to call the the ArrayList because we call this one in the Collision Void
if (lyr.locked) {
return true;
}
}
return false;
}
//
// see if there is collide between two things
boolean collide(Layer target)
{
float distanceX = newPosX - target.newPosX ;
float distanceY = newPosY - target.newPosY ;
float treshX = s.x /2.0 + target.s.x /2.0 ;
float treshY = s.y /2.0 + target.s.y /2.0 ;
// println(abs(distanceY) + " / " + treshY) ;
if (abs(distanceX) < treshX && abs(distanceY) < treshY) { // if the distance is less than the threshold, we are colliding!
return true;
} else {
return false;
}
}
///////////////
}
Thanks
Stan
Stan
1