Solved

Sorting array of coordinates

Posted on 2008-10-19
4
2,073 Views
Last Modified: 2012-08-13
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.
0
Comment
Question by:ubuntuguy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 4

Accepted Solution

by:
petr_hlucin earned 500 total points
ID: 22753506
What you should do is use the following version of Sort:
static void sort(Object[] a, Comparator c);

The Comparator and Coordinate would be the following in your case.
class CoordinageComparator implements Comparator{
 
  public int compare(Coordinate coordinate1, Coordinate coordinate2) {
    return coordinate1.compareTo(cordinate2);
  }
}
 
class Coordinate {
  public double X;
  public double Y;
 
  public CompareTo(Coordinate c) {
    if (c.X < this.X)
      return -1;
    else if (c.X > this.X)
      return 1;
    else 
      return 0;
  }
}

Open in new window

0
 
LVL 1

Author Comment

by:ubuntuguy
ID: 22753744
Hmmm, I thought about doing it this way, but my method must run in O(nlogn) and this would run in O(n^2).  I know the Arrays.sort() runs in O(nlogn), but I'm unsure on how to implement it.
0
 
LVL 4

Expert Comment

by:petr_hlucin
ID: 22753807
IMHO this would run in O(n*log(n)) - CompareTo() runs in O(1) and Sort by itself runs in O(n*log(n)). If you think that sort with Comparator runs in O(n^2) you may write your own sort :-). No, seriously I'm almost sure that Sort with Comparator runs in O(n*log(n)) and therefore the following code together with the one posted with my previous post should solve your problem in O(n*log(n)).
Coordinate[] c = new Coordinate[50];
 
Arrays.Sort(c, new CoordinateComparator());

Open in new window

0
 
LVL 1

Author Closing Comment

by:ubuntuguy
ID: 31507638
thanks, you were right.  
0

Featured Post

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
ejb stateless example 2 66
java example issue 5 63
junit example issue 2 54
Eclipse neon2 "Java build path" correctness 7 43
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Iteration: Iteration is repetition of a process. A student who goes to school repeats the process of going to school everyday until graduation. We go to grocery store at least once or twice a month to buy products. We repeat this process every mont…
The viewer will learn how to implement Singleton Design Pattern in Java.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

737 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question