如何比较排序(x,y)点,其中x始终在前。

huangapple go评论80阅读模式
英文:

How to compare (x,y) points for sorting, where x is always first

问题

使用Java中的Comparator来比较具有坐标点(x,y)的位置对象。

我需要能够比较两个点,以获取一个正整数或负整数,使我能够对(x,y)点进行排序,其中首先对x值进行排序,然后对y值进行排序。(如果这样说得通的话...)
例如,这个:

(3,4) (2,5) (1,1) (1,3) (3,3)

变成了这个:

(1,1) (1,3) (2,5) (3,3) (3,4)

我思考过的一种方法实际上是通过将x值乘以一个大数值(比如1000)来赋予x值更高的优先级。像这样:
比较(3,3)和(1,1):

int x_multiplier = 1000;
int value1 = (p1.x * x_multiplier)+ p1.y; // = 3 * 1000 + 3 = 3003
int value2 = (p2.x * x_multiplier)+ p2.y; // = 1 * 1000 + 1 = 1001
return value1-value2; // = 2002。Value1较大,因此p1会在列表中位于p2之后。

这种方法是有效的,但问题在于,如果y值等于或大于x_multiplier,那么这种方法会失效(因为该y值现在等于1个x值...如果这样说得通的话)。

// 比较 p1 =(2,0)和p2 =(1,18)
int x_multiplier = 10;
int value1 = (p1.x * x_multiplier)+ p1.y; // = 2 * 10 + 0  = 20
int value2 = (p2.x * x_multiplier)+ p2.y; // = 1 * 10 + 18 = 28
return value1-value2; // = -8,value2较大,因此p2将会在列表中位于p1之后。然而,通过查看这些点,我们知道p2应该在p1之前。

我实际上甚至不知道如何搜索这个问题,所以如果有答案,我也找不到。

英文:

I'm using Java Comparator to compare location objects with points (x,y).

I need to be able to compare two points to retrieve a positive or negative integer that will allow me to sort (x,y) points, where x-values are sorted first, then y-values secondly. (If that makes sense...)
For example, this:

(3,4) (2,5) (1,1) (1,3) (3,3)

Becomes this:

(1,1) (1,3) (2,5) (3,3) (3,4)

One way I've thought to do it is essentially by giving the x-value large precedence by multiplying it by a large number like 1000. Like so:
Comparing (3,3) and (1,1):

int x_multiplier = 1000;
int value1 = (p1.x * x_multiplier ) + p1.y; // = 3 * 1000 + 3 = 3003
int value2 = (p2.x * x_multiplier ) + p2.y; // = 1 * 1000 + 1 = 1001
return value1-value2; // = 2002. Value1 is greater, thus p1 be later in list.

This works, but the issue with this is if the Y-value should ever be equal to or greater than the x_multiplier, then this breaks down (because that y-value is now equal to 1 x-value... again, if that makes sense.)

// Comparing p1 = (2,0) & p2 = (1,18)
int x_multiplier = 10;
int value1 = (p1.x * x_multiplier ) + p1.y; // = 2 * 10 + 0  = 20
int value2 = (p2.x * x_multiplier ) + p2.y; // = 1 * 10 + 18 = 28
return value1-value2; // = -8, value2 is greater, and thus p2 will be later in the list. However, we know by looking at the points that p2 should come first.

I don't really even know how to search for this, so if there are answers out there I couldn't find them.

答案1

得分: 0

import java.util.*;

class Tuple {
    public int x, y;

    Tuple(int x, int y) {
        this.x = x;
        this.y = y;
    }

    @Override
    public String toString() {
        return "(" + x + ", " + y + ") ";
    }
}

public class coord {
    public static void main(String[] args) {
        LinkedList<Tuple> list = new LinkedList<Tuple>();
        list.add(new Tuple(3,4));
        list.add(new Tuple(2,5));
        list.add(new Tuple(1,1));
        list.add(new Tuple(1,3));
        list.add(new Tuple(3,3));
        for (Tuple t: list) {
            System.out.print(t);
        }
        Collections.sort(list, (Tuple t1, Tuple t2) -> {
            int result = Integer.compare(t1.x, t2.x);
            if (result == 0 ) result = Integer.compare(t1.y, t2.y);
            return result;
        });
        System.out.println("Sorted List: ");
        for (Tuple t: list) {
            System.out.print(t);
        }
    }
}
英文:
import java.util.*;
class Tuple {
public int x, y;
Tuple(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public String toString() {
return &quot;(&quot; + x + &quot;, &quot; + y + &quot;) &quot;;
}
}
public class coord {
public static void main(String[] args) {
LinkedList&lt;Tuple&gt; list = new LinkedList&lt;Tuple&gt;();
list.add(new Tuple(3,4));
list.add(new Tuple(2,5));
list.add(new Tuple(1,1));
list.add(new Tuple(1,3));
list.add(new Tuple(3,3));
for (Tuple t: list) {
System.out.print(t);
}
Collections.sort(list, (Tuple t1, Tuple t2) -&gt; {
int result = Integer.compare(t1.x, t2.x);
if (result == 0 ) result = Integer.compare(t1.y, t2.y);
return result;
});
System.out.println(&quot;Sorted List: &quot;);
for (Tuple t: list) {
System.out.print(t);
}
}
}

huangapple
  • 本文由 发表于 2020年9月3日 11:01:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/63716160.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定