我有一个二维数组,表示构成矩形的点网格的 x 和 y 坐标。使用的数据集通常非常大。我想对从左上角开始并沿平行对角线移动直到右下角的点进行排序。我使用 Arrays.sort 函数和以下比较器来执行此操作:
public int compare(Object o1, Object o2) {
double[] a1 = (double[])o1;
double[] a2 = (double[])o2;
if (a1[0]+a1[1] > a2[0]+a2[1]) return 1;
else if (a1[0]+a1[1] < a2[0]+a2[1]) return -1;
else {
if (a1[0] > a2[0]) return 1;
else if (a1[0] < a2[0]) return -1;
else return 0;
}
}
当每个点的 x 和 y 坐标间隔两位数字时,由于某种原因,代码可以工作,但如果它们间隔,则排序会出现错误仅相差一位数字。
原始订单的示例可以在此处找到:
http://www.mediafire.com/?slq73v3zn2zs98l
以及生成的排序列表的示例看起来可以在这里找到:
http://www.mediafire.com/?x8f08q0qoof398w
为什么排序不起作用?任何帮助将非常感激!
I have a 2-D array respresenting the x and y coordinates of a grid of points making up a rectangle. The data sets being used are typically very large. I want to sort the points starting in the top left hand corner and moving in parallel diagonal lines until the bottom right hand corner. I'm using the Arrays.sort function and the following comparator to do it:
public int compare(Object o1, Object o2) {
double[] a1 = (double[])o1;
double[] a2 = (double[])o2;
if (a1[0]+a1[1] > a2[0]+a2[1]) return 1;
else if (a1[0]+a1[1] < a2[0]+a2[1]) return -1;
else {
if (a1[0] > a2[0]) return 1;
else if (a1[0] < a2[0]) return -1;
else return 0;
}
}
The code works for some reason when the x and y coordinates of each point are spaced two digits apart, but there are errors in the sort if they're spaced only one digit apart.
An example of the original order can be found here:
http://www.mediafire.com/?slq73v3zn2zs98l
And an example of what the resulting sorted list is looking like can be found here:
http://www.mediafire.com/?x8f08q0qoof398w
Why isn't the sort working? Any help would be so very much appreciated!
发布评论
评论(1)
看看您链接到的图片,我认为您想在 Double.compare() "nofollow noreferrer">该点距左上角的L1距离。
这意味着,给定 2d 点
a
和b
,以及您想要比较的“原点”Origin
(在本例中为左上角点) :并通过比较这些距离进行排序。您可以将其包装到使用
Double.compare()
的合理比较器中。更新:在您上次发表评论后,我现在了解了额外的排序标准。在比较双精度数时,您可能需要考虑精度问题(其他人可以为您提供更好的建议) ),但是你想要的东西大致如下:
Looking at the picture you have linked to, I think you want to do
Double.compare()
on the L1 distance of the point from the top left corner.This means, given 2d points
a
andb
, and "origin"Origin
(in this case the top-left point) you want to compare:and sort by comparing these distances. You could wrap this up into a sensible comparator that uses
Double.compare()
.Update: I understand the extra sorting criteria now, after your last comment. You may want to consider precision issues when comparing doubles (someone else will be able to advise you better), however you want something roughly like: