*
The moose likes Java in General and the fly likes Sorting an ArrayList<Point> Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Sorting an ArrayList<Point>" Watch "Sorting an ArrayList<Point>" New topic
Author

Sorting an ArrayList<Point>

Ryan Callen
Greenhorn

Joined: Nov 29, 2012
Posts: 22
I wrote a simple program which reads in a list of points from a text file and arranges them into an ArrayList. I wrote a method which calculates the absolute distances (cab distance) of each point (absolute value of the x value plus the absolute value of the y value). I also wrote another method which prints the x and y value of the points in the ArrayList along with the cab distance of the points. What I want to do now is write a method that will accept the ArrayList with the points and sort the points once again by their cab distance. It will be much like the first method, but the points will be sorted when they are printed. Any ideas? My code is attached. Thanks!

CODE:

[Added code tags - see UseCodeTags for details]

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36482
    
  16
Surely the cabDistance() method should be an instance method of the point class? And the x and y fields should have private access. Initialise them via the constructor, and don’t use (0, 0) as a default.
I presume you are familiar with the appropriate Java Tutorials Section. That has a part about ordering, which will help you. Simply, you need to decide that points have a natural ordering (which they probably haven’t) and you can implement the Comparable interface, or you can decide that some feature allows you to apply an ordering, maybe the cab distance, and you can create a Comparator object which orders your points by cab distance. In which case points (4, 5), (5, 4) and (6, 3) will count as equivalent to one another.
Ryan Callen
Greenhorn

Joined: Nov 29, 2012
Posts: 22
I was thinking about trying to use some type of insertion sorting or a selection sort. In other words, I want to refrain from using Comparator. I want to keep the method void and only pass in an ArrayList. I know that points have natural ordering, however, the cabDistance of each is an absolute distance. This differs with their natural distance. Would i be able to create a new arrayList and sort the points back into there and then print that list?
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7052
    
  16

Ryan Callen wrote:I was thinking about trying to use some type of insertion sorting or a selection sort. In other words, I want to refrain from using Comparator...

Why? It's the natural way to do this; and its method can be implemented in one line.

Then you can sort any List<Point> you like using Collections.sort().

Or is your real question about how to write a sort?

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Artlicles by Winston can be found here
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Sorting an ArrayList<Point>
 
Similar Threads
' + ' Operator problem
pass by reference and value
Help on zooming in on an image
I tried to draw Arrow from Node to another Node object. but its not working
Increment on object reference ?