如何按多个字段对记录列表进行排序?

fru*_*ugi 1 delphi sorting

我想首先按x坐标排序坐标列表,然后按y坐标排序:

Orginal: (7,8)(10,22)(7,3)(5,10)(20,14)(7,10)(7,3)

First Step by x: (5,10)(7,8)(7,3)(7,10)(7,3)(10,22)(20,14)

Second Step by y: (5,10)(7,3)(7,3)(7,8)(7,10)(10,22)(20,14)
Run Code Online (Sandbox Code Playgroud)

我已经有一个适用于第一步的功能:

function SortCoords(Item1: Pointer; Item2: Pointer): Integer;
var
line1 : Coords;
line2 : Coords;
begin
  line1 :=  Coords;(Item1);
  line2 :=  Coords;(Item2);

  if (line1.X < line2.X) then 
    result := -1
  else if (line1.X > line2.X) then 
     result := 1
  else
     result := 0;

end; 
Run Code Online (Sandbox Code Playgroud)

但我没有第二步.

Jas*_*onD 7

如果您想要按辅助键排序的项目,这只有在第一个键相等时才会很重要.在您的示例中,就是这种result := 0;情况.

所以像这样:

if (line1.X < line2.X) then 
    result := -1
  else if (line1.X > line2.X) then 
     result := 1
  else if (line1.Y < line2.Y) then 
    result := -1
  else if (line1.Y > line2.Y) then 
     result := 1
  else
     result := 0;
Run Code Online (Sandbox Code Playgroud)

可能做你想要的.