Solved

# Sorting array of coordinates

Posted on 2008-10-19

Hi, I have a collection of coordinates x, y (doubles) i.e. (3.4, 5.3), (3.0, 5.5), and so on...

I put them into an array and I need to sort them based on either coordinate. I need to use the sort method from the java.util.Arrays class.

I imported the class, created an array in which I put all my coordinate objects. I'm not sure how the Arrays.sort() method works. How do I go about sorting the array based on either coordinate (x or y).

Let's say that I have :

Coordinate [] myArray = {c1, c2, c3, c4, ...cn} where each c is a coordinate i.e c1 = (3.4, 5.5)

Once again I need to use the built in method in the Arrays class.

Would I have to do something like,

Arrays.sort(myArray.getX()) getX() gets the x cordinate

in order to sort all the coordinates in increasing order of the X coordinate?

Sorry if this seems a trivial question, but even when I create an array on int's (int [] myArray), and I do Arrays.sort(myArray), the code doesn't compile.

p.s. I need to use the Arrays.sort() method, no need to create my own implementation.