2

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?

0

2 Answers 2

2

The comparator is the proper way to go. You could sort you arrays of x and y values so that the ending array of Points is sorted, but I think that making your point class implement the Comparable interface is better and less error prone.

class Point implements Comparable<Point>{
    public float x;
    public float y;
    
    @Override
    public int compareTo(Point o) {
       if(this.x>o.x){
           return 1;
       }else if(this.x==o.x){
           return 0;
       }else{
           return -1;
       }
    }
}

Then you can just call

Arrays.sort(graphPoints); to sort it.

Switching the 1 and -1 return values will switch whether it sorts in Ascending or Descending order.

Explanation

What this does is when you call the Arrays.sort method, it will compare every point in the array by using the compareTo method. If compare to returns a 1, the sort method knows that point is considered greater than the other one it compared. If its 0, they are equal. And if its -1, then it is less. This the standard way to go and makes it easy to sort an Array without having to write the sorting code yourself, which would probably be messy and inefficient.

Further, it makes it easy to change the behavior of the sorting simply by changing your compareTo method. You can change whether it is in ascending or descending order, as well as what you are sorting by. For example, if you want to you can easily change whether it sorts by the x or the y value just by changing compareTo in one place.

Sign up to request clarification or add additional context in comments.

1 Comment

This is a fine solution, but in the purest sense the Comparable interface should be implemented by classes whose instances have a sense of natural order, i.e. a clearly stated contract of how instances are ordered. This is very intuitive for classes like BigInteger whose values clearly have a natural ordering, but not so clear for Point instances since there are many ways in which they could be ordered. An arguably clearer solution would be to use a separate Comparator instance as a strategy and sort explicitly with that pattern.
0

You can use Arrays#sort(T[],Comparator) method as follows:

Arrays.sort(graphPoints, Comparator.comparing(Point::getX));

In this case, you have to add the getter method to the Point class:

class Point {
    public float x;
    public float y;

    public float getX() {
        return x;
    }

    public float getY() {
        return y;
    }
}

See also: How to sort by a field of class with its own comparator?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.