Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
108 views
in Technique[技术] by (71.8m points)

java - Sorting an object array on a float value

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?

question from:https://stackoverflow.com/questions/65836043/sorting-an-object-array-on-a-float-value

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...