Solved

# sort coordinates

Posted on 2003-03-16
Medium Priority
1,187 Views
hello, i have a set of corrdinates which i want to put them into a matrix. i wish to sort them according to their x coordinates. eg, (2,5), (6,7), (3,4), (0,9) will be sorted to (0,9), (2,5), (3,4), (6,7). the java.util.Array.sort can onli sort a 1D array. how do i do this such tt it corresponds to the matlab command "sortrows()" ?
thx!!

rain
0
Question by:rain81
[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
• 7
• 4
• 2
• +1

LVL 2

Expert Comment

ID: 8149643
what do you want to do when you run into (5,6) and (5,9)?
0

Author Comment

ID: 8149748
(5,6) then (5,9). aniwas my main purpose i8s to sort the x-axis so the y axis wun really affect much. thx! ^_^
0

LVL 8

Accepted Solution

akshayxx earned 320 total points
ID: 8149950
if u have objects of coordinates then u can sort by using following function from
java.util.Arrays.sort , u will have to provide an instance of a class which implements Comparator interface..
public static void sort(Object[]Â a,ComparatorÂ c)

look at java.util.Comparator for more information .. the description there is very similar to ur case
0

LVL 2

Expert Comment

ID: 8149959
make your own class and implement Comparator.
then in the compare method define your own way of comparision.

the compare methods will recieve two objects (obviuosly of Matrix type) this way u can compare the x-axis.

public class Matrix {
private int x, y;
public Matrix(int xx, int yy) {
x = xx;
y = yy;
}

public int getXaxis() {
return x;
}

public int getYaxis() {
return y;
}
}

public class MySort implements Comparator {
public MySort() { }
public int compare(Object o1, Object o2) {
if (o1 instanceof Matrix && o2 instanceof Matrix) {
Matrix m1 = (Matrix)o1;
Matrix m2 = (Matrix)o2;

int x1 = m1.getXaxis();
int x2 = m2.getXaxis();

if (x1 > x2) return 1;
if (x1 < x2) return -1;
return 0;
}
return super.compare(o1, o2);
}

public boolean equals(Object o) {
return super.equals(o);
}
}

public class Test {
public static void main(String[]args) {
Vector v = new Vector();
Collections.sort(v, new MySort());
}
}
0

LVL 8

Expert Comment

ID: 8149970
here is an example from google .. to show u how to define Comparator class .. once u define such a class for your coordinate object..
then  u can pass the instance of that Comparator class to java.util.Arrays.sort
like this
PointClass points[];
//fill up the points
java.util.Arrays.sort(points,myComparatorObject);
try implementing ur own comparator .. if u r stuck let us know

import java.util. Comparator;
class Student  {
String name;
int id;

public Student( String newName, int id ) {
name = newName;
this.id = id;
}

public String toString() {
return name + ":" + id;
}
}
final class StudentNameComparator implements Comparator {

public int compare( Object leftOp, Object rightOp ) {
String leftName = ((Student) leftOp).name;
String rightName = ((Student) rightOp).name;
return leftName.compareTo( rightName );
}

public boolean equals( Object comparator ) {
return comparator instanceof StudentNameComparator;
}
}
0

LVL 8

Expert Comment

ID: 8149979
glottis:
most of the experts here can provide the full source code .. but we here follow EE policy of providing information good enough to get the questioner started ..
as we may never know whether the questioner is asking for school home work or not..
please try to follow such rules in future..
0

LVL 8

Expert Comment

ID: 8149983
also i doubt u can have so many "public class" in single source file
0

Author Comment

ID: 8150268
akshayxx/glottis , can you explain more about comparators to me? i haven used it b4. and my data is extremely big( ard 16,000 coordinates), is it feasible to compare them one by one? you juz nid to explain to me the concepts, a full source is not necessary. cheers!

rain ^_^
0

LVL 8

Expert Comment

ID: 8150340
to sort any set of records .. u need any function or operator which can compare the two records of this type ..
say u have 2 objects of type point

and if u r asked to sort these two .. then u'll need to compare these two objects ..
thats what a comparator function is ..

in ur case u wud do
if(point1.x > point2.x ) then return something which means point1 is greater than point2

as per the Comparator interface .. that 'something' has to be

a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.

and u must also implement other function of the interface that will tell u when shud the two objects be considered as equal in order

any Sorting algorithm works on this basis
in case of sorting integers u need to have < and > and = operators working

and here u implement the comparator interface ..
hope u got the idea..
0

Author Comment

ID: 8157043
thx! i've managed to implement a simple comparator, however theres a problem. when i added the coordinates to a vector, ie. v.addElement(new Point(2,3)), when i use System.out.println(v), i was unable to view the coordinates, instead, some rubbish is being displayed out. am i suppose to parse it to anything such that (2,3) would be displayed out? if so, any hints?

thx! ^_^

rain
0

LVL 8

Expert Comment

ID: 8157336
u have to print the elements on vector ..one by one .. u cant just print vector like that..
best u can do is .. in ur Point Class define a method
String toString(){
// compose the string . from ur coordinates and return the string

}

with this u will be able to do

System.out.println(v.get(someIndex));
OR
System.out.println(new Point(2,3));

0

LVL 2

Expert Comment

ID: 8157428
u can iterate through a for loop, from 0 to v.size() then print like System.out.println(v.elementAt(index))
0

Author Comment

ID: 8157505
it works! finally understood how to use comparator. thx akshayxx and glottis. guess i'll gif the points to akshayxx coz he ans first. hope u dun mind glottis. ^_^
thx pple, ur explanations really help alot

rain
0

LVL 8

Expert Comment

ID: 8157636
good to know it worked..
u cud have asked for split of points..
anyways lets hope glottis didnt mind
0

## Featured Post

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilationâ€¦
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the bâ€¦
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a vâ€¦
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
###### Suggested Courses
Course of the Month14 days, 14 hours left to enroll

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

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