Results 21 to 27 of 27
Thread: Java Sorting Array of points!
- 10-27-2010, 02:59 AM #21
Senior Member
- Join Date
- Sep 2010
- Posts
- 109
- Rep Power
- 0
Ok, and how do i do that? haha. and fubarable, this is what happens when you help people who have less developed methods (; Hopefully, when ill be better and eventually be able to use class methods directly, and work around a lot of these minor problems. after all, im only 16, ive got along time...i hope XD
- 10-27-2010, 03:20 AM #22
Senior Member
- Join Date
- Sep 2010
- Posts
- 109
- Rep Power
- 0
Haha Helppppp!
- 10-27-2010, 03:34 AM #23
Senior Member
- Join Date
- Sep 2010
- Posts
- 109
- Rep Power
- 0
By the way, i realize that i could just do this:
But, i am would like to know how to do it the skimed down way, that im having trouble with. ill just sub this in till i figure it out, but i really rally really want to know the other way.Java Code:Comparator<Point> pointorder = new Comparator<Point>(){ public int compare(Point p1, Point p2) { double sort = p1.getX() - p2.getX(); if(sort < 0) { return -1; }else if(sort > 0){ return 1; }else{ return 0; } } };
- 10-27-2010, 03:36 AM #24
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
That method looks good to me. There isn't a "skimmed down" way except to do the subtraction with the x and y fields.
Do you know what Fubarable and I mean by those fields?Last edited by pbrockway2; 10-27-2010 at 03:38 AM. Reason: Didn't see previous post...
- 10-27-2010, 03:39 AM #25
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Also maybe you could post where you are now. I mean how is that comparator being used, and is it giving the sorted order you expect?
- 10-27-2010, 03:55 AM #26
Senior Member
- Join Date
- Sep 2010
- Posts
- 109
- Rep Power
- 0
Well, i will give you the version im on, as soon as im finished with it! im almost there! also, im not sure if i understand what you mean by those fields. and yes, im getting what i want, ive put it through some ordering tests and its working!
- 10-27-2010, 04:07 AM #27
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Great. The following isn't intended to sidetrack you - better to stick with something that does what you want.
The Point class offers methods like getX() and getY() that return coordinates.
Java Code:Point p = new Point(1, 2); double foo = p.getX(); // foo is now the x-coordinate of point p
But there are also x and y. They aren't methods, so you don't use parentheses. They allow you to directly access the point's data:
Java Code:Point p = new Point(1, 2); // p.x is now the x-coordinate of point p
Consider the expression "p1.x - p2.x". It has the right sign to be returned by compare() and it's of the right type (int).
Similar Threads
-
Help with java sorting two dimensional array
By Joycey in forum New To JavaReplies: 2Last Post: 03-27-2010, 02:36 AM -
Sorting Array
By saqib15 in forum New To JavaReplies: 1Last Post: 02-12-2010, 03:42 AM -
Sorting an Array via Stacks
By viperlasson in forum New To JavaReplies: 0Last Post: 02-01-2010, 06:53 AM -
given number of points(cordinates) , find max points lie on the same line ?
By Hayzam in forum New To JavaReplies: 2Last Post: 08-24-2008, 12:30 AM -
Sorting an array of Strings
By Java Tip in forum java.langReplies: 0Last Post: 04-15-2008, 07:39 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks