大数据集排序问题

发布于 2024-11-03 10:55:00 字数 836 浏览 4 评论 0 原文

我有一个二维数组,表示构成矩形的点网格的 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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

薄凉少年不暖心 2024-11-10 10:55:00

看看您链接到的图片,我认为您想在 Double.compare() "nofollow noreferrer">该点距左上角的L1距离

这意味着,给定 2d 点 ab,以及您想要比较的“原点”Origin(在本例中为左上角点) :

distance_a = Math.abs(Origin.x - a.x) + Math.abs(Origin.y - a.y)
distance_b = Math.abs(Origin.x - b.x) + Math.abs(Origin.y - b.y)
return Double.compare(distance_a,distance_b);

并通过比较这些距离进行排序。您可以将其包装到使用 Double.compare() 的合理比较器中。

更新:在您上次发表评论后,我现在了解了额外的排序标准。在比较双精度数时,您可能需要考虑精度问题(其他人可以为您提供更好的建议) ),但是你想要的东西大致如下:

distance_a = Math.abs(Origin.x - a.x) + Math.abs(Origin.y - a.y)
distance_b = Math.abs(Origin.x - b.x) + Math.abs(Origin.y - b.y)
if distance_a is not equal to distance_b // check how you should perform the comparison
    return Double.compare(distance_a,distance_b);
else
    return Double.compare(a.x,b.x);

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 and b, and "origin" Origin (in this case the top-left point) you want to compare:

distance_a = Math.abs(Origin.x - a.x) + Math.abs(Origin.y - a.y)
distance_b = Math.abs(Origin.x - b.x) + Math.abs(Origin.y - b.y)
return Double.compare(distance_a,distance_b);

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:

distance_a = Math.abs(Origin.x - a.x) + Math.abs(Origin.y - a.y)
distance_b = Math.abs(Origin.x - b.x) + Math.abs(Origin.y - b.y)
if distance_a is not equal to distance_b // check how you should perform the comparison
    return Double.compare(distance_a,distance_b);
else
    return Double.compare(a.x,b.x);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文