Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Algorithm 按两个字段排序索引_Algorithm_Sorting - Fatal编程技术网

Algorithm 按两个字段排序索引

Algorithm 按两个字段排序索引,algorithm,sorting,Algorithm,Sorting,我有一个对象,它有两个字段x和y。X代表价格,Y代表预算。我想在上面生成一个数字,我可以对一个列表进行排序,这样具有最大价格和最大预算的对象就会出现在列表的开头 [ { "x": 3, "y": 5 }, { "x": 2, "y": 1 }, { "x": 5, "y": 4 }, { "x": 1, "y": 7 }, { "x": 5, "y": 7 } ] 应该是: [ { "x": 5, "y": 7 }, { "x": 5,

我有一个对象,它有两个字段x和y。X代表价格,Y代表预算。我想在上面生成一个数字,我可以对一个列表进行排序,这样具有最大价格和最大预算的对象就会出现在列表的开头

[
    { "x": 3, "y": 5 },
    { "x": 2, "y": 1 },
    { "x": 5, "y": 4 },
    { "x": 1, "y": 7 },
    { "x": 5, "y": 7 }
]
应该是:

[
    { "x": 5, "y": 7 },
    { "x": 5, "y": 4 },
    { "x": 3, "y": 5 },
    { "x": 2, "y": 1 },
    { "x": 1, "y": 7 }
]

我想你要问的是,你想按x然后y排序。通常的方法是实现自己的比较函数,而不知道编程语言,它可能看起来像这样

function compare(A, B):
    if(A.x > B.x or A.x == B.x and A.y > B.y)
         return A
    return B