z *_* - 8
根据您的订购,看起来您将y位置放在比x位置更高的优先级,所以这样的事情在比较两个人时会起作用:
if (a.y > b.y)
// a is before b
else if (a.x < b.x)
// a is before b
else
// b is before a
Run Code Online (Sandbox Code Playgroud)
编辑更新 此比较仍适用于您的新条件.Y位置仍然优先于X位置.如果Y值相等,则最靠近左上角的点将是具有较小X值的点.如果你想让你的对象成为比较器,那么将它作为比较器函数实现将允许你做ArrayList.sort(),其中negative表示第一个人在第二个人之前:
public int compareTo(person a, person b) {
if (a.y == b.y)
return a.x-b.x
else
return b.y-a.y
}
//compareTo(Tom, Harry) == -50 (tom is before harry)
//compareTo(Tom, Bob) == -25 (tom is before bob)
//compareTo(Dave, Bob) == 30 (dave is after bob)
Run Code Online (Sandbox Code Playgroud)