Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Sorting an ArrayList<Point>

 
Ryan Callen
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10226
58
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic