I want to sort many arrays in many different ways

for one of my projects, I have two arrays, one storing X coordinates, and one with Y coordinates.
my goal is to sort the X coordinates from least to greatest using the “sort” function. the problem is, then none of the X coordinates no longer correspond to the correct Y cords. my current plan is to write my own sorting function that’s easier to customize.

the code is pretty messy, and it’ll take a while to explain.

int reps = 6;                             //the total ammount of bezier curves (rounds up to nearest even number)
float vDist = 20;

float dx = 50;                             //horizontal distance from the control point to the anchor point
float dy = 30;                             //vertical distance from the control point to the anchor point

float xScale = 175;                        //amount of pixels from the left to the right of the ellipse
float yScale = 50;                         //amount of pixels from the left to the right of the ellipse

float pointX = 0;                          //the X cord of the tip of the ghosts head
float pointY = -200;                       //the Y cord of the tip of the ghosts head

int res = 4;                                //the ammount of points per bezier to find

int circleR = 30;
float [] circleX = new float [reps*res];    //array used to find x cords of lines at semi-circle
float [] circleY = new float [reps*res];    //array used to find y cords of lines at semi-circle


float [] lineX = new float [reps*res];      //array used to find x cords of lines at bezier 
float [] lineY = new float [reps*res];      //array used to find y cords of lines at bezier 

float [] lineXs = new float [reps*res];     //sorted (least to greatist) version of lineX array
float [] lineYs = new float [reps*res];     //sorted (least to greatist) version of lineY array

float small = 3/4;



void setup() {
  size(512, 720);
  colorMode (HSB);
  background(255);
  smooth(8);

  resetMatrix();
  stroke(0,0,0,10);
  strokeWeight(1);
  
   
  strokeWeight(1);
  stroke(0);
  
  background(255);
  translate(width/2, height/4*3); // changes the origin to near the center.
  noFill();


  for (int i = 1; i <= reps; i++) {
    
    
    
    float xPos = cos(i*PI/(reps/2));
    float yPos = sin(i*PI/(reps/2));

    float xPos1 = cos(((i-1)*PI)/(reps/2));
    float yPos1 = sin(((i-1)*PI)/(reps/2));

    //float xPos2 = cos(((i+1)*PI)/(reps/2));
    //float yPos2 = sin(((i+1)*PI)/(reps/2));


    float x1 = xPos*xScale;
    float x2 = xPos*xScale+dx;
    float x3 = xPos1*xScale-dx;
    float x4 =  xPos1*xScale;
    
    float y1 = yPos*yScale;
    float y2 = yPos*yScale+dy;
    float y3 = yPos1*yScale-dy;
    float y4 = yPos1*yScale;
    


    ellipse (x1, y1, 1, 1); // anchor point
    ellipse (x3, y3, 1, 1); // left control point
    ellipse (x2, y2, 1, 1); // right control point


    bezier (x1, y1, x2, y2, x3, y3, x4, y4);      //drawing the curve


    for (int u = 1; u <= res; u++) {
      float t = u / float(res);
      lineX[res*(i-1)+(u-1)] = bezierPoint(x1, x2, x3, x4, t);      //storing the x position for every point on bezier
      lineY[res*(i-1)+(u-1)] = bezierPoint(y1, y2, y3, y4, t);      //storing the y position for every point on bezier
   

}

 lineXs = sort (lineX);
 printArray(lineX);
 print("\n");
 printArray(lineXs);
 
 }
 
   for (int o = 0; o < res * reps; o++) {

     float pY = circleY[o]*circleR+pointY;                      //instance of x cord on the bezier
     float pX = circleX[o]*circleR+pointX;                      //instance of y cord on the bezier
     
    line(pX, pY, lineXs[o], lineYs[o]);                      //ploting the current instance 
    
  }
}

should I just write my own sorting function that is compatible with what I am trying to do, or is there an easier way?

1 Like

The way I see it you work with parallel arrays.

So each data is distributed over two arrays. Therefore you cannot sort one alone but the connected list must change as well.

One way is that you use a class (define a data structure) that holds x and y. Then you make a array of that class.
Actually that’s basically PVector.

Or make your own class, see text tutorials objects

Then see How can I sort JSON by ID? - #9 by Chrisir

I think my approach is too complicated

Try yours

I’d personally store coordinates as a list of PVectors (rather than across two arrays).
You can then use .sort() method on the list and supply your own comparison function:

List<PVector> circleCoords = new ArrayList<>();
...
circleCoords.sort((a, b) -> Float.compare(a.x, b.x));
1 Like