java - Do anyone know how to sort stack using array.sort()? I just want to print the ordered list of points by their x coordinates. Any idea? -
import java.util.*; public class point2d { private double x; private double y; public point2d(double x, double y) { this.x = x; this.y = y; } private double euclidean() { double distance = math.sqrt((x * x) + (y * y)); return distance; } public string tostring() { return "x: " + x + ", y: " + y; } public static class yorder implements comparator<point2d> { public int compare(point2d self, point2d other) { if (self.y > other.y) return 1; if (self.y < other.y) return -1; else return 0; } } public static void main(string[] args) { stack<point2d> stack = new stack<point2d>(); while (!stdin.isempty()) { double x = stdin.readdouble(); double y = stdin.readdouble(); stack.push(new point2d(x,y)); } arrays.sort(stack); } }
this class , i'm not allowed use collections
or arraylist
.
arrays.sort(stack);
should be
collections.sort(stack, new yorder());//pass comparator
Comments
Post a Comment