I'm beginning to learn Java. Currently I got stuck at sorting an array of Objects in ascending order of a float value in the object. Goal is to draw a Graph from the arrays of data. Before I can draw the graph I have to sort the coordinates so that the x values are ascending order. So I put them in an Object to link x and y data together.
I'm starting with 2 arrays of floats:
float[] xValues = {-5.6f, 2.1f, 0.1f, 4.4f, 8.5f, -1.6f};
float[] yValues = {1.0f, 2.4f, 5.7f, -2.8f, -6.4f, 2.9f};
Than I put it into an Point object array using this function:
graphPoints = new Point[arrayLength];
for (int i = 0; i < graphPoints.length; i++) {
graphPoints[i] = new Point();
}
for (int i = 0; i < graphPoints.length; i++) {
graphPoints[i].x = x[i];
graphPoints[i].y = y[i];
}
containing this object:
class Point {
public float x;
public float y;
}
Possible solutions I found on the web suggest sorting the object array using a comparator<Point>, something I haven't learned yet. Using the examples I found could not get it to work with an float value. All examples work with either strings of int's.
Am I missing something? Is there a proper way to do this? Who could give me some guidance on this topic?