2012-12-17 59 views
1

我想先通過它的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) 

我已經說了第一步工作的功能:

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; 

但我沒有得到第二步。

回答

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; 

可能會做你想要什麼。

+0

thx工作。 – frugi

2
type 
TPC=Class 
    x,y:Integer; 
    Constructor Create(ax,ay:Integer); 
End; 


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

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

procedure TForm4.Button1Click(Sender: TObject); 
var 
    l:TList; 
    I: Integer; 

begin 
    l := TList.Create; 
    try 
    l.Add(TPC.Create(7,8)); 
    l.Add(TPC.Create(10,22)); 
    l.Add(TPC.Create(5,10)); 
    l.Add(TPC.Create(20,14)); 
    l.Add(TPC.Create(7,10)); 
    l.Add(TPC.Create(7,3)); 
    l.Sort(SortCoords); 
    for I := 0 to l.Count- 1 do 
     begin 
     memo1.lines.add(Format('x: %d y: %d',[TPC(l[i]).x,TPC(l[i]).y])); 
     TPC(l[i]).Free; 
     end; 
    finally 
    l.Free; 
    end; 

end; 

{ TPC } 

constructor TPC.Create(ax, ay: Integer); 
begin 
    x := ax; 
    y := ay; 
end; 
+0

你的工作也是如此,但JasonD速度更快。抱歉。 – frugi

+0

對於這樣簡單的數據,'TPC = record'可能會更好。 –

2

您可以直接使用'開箱即用'Sort方法。

輸出由

// data initialization 

Dump; 

TArray.Sort<Coords1>(c1); 
TArray.Sort<Coords2>(c2); 
c3.Sort; 

Dump; 

7: [ (7; 8), (10; 22), (7; 3), (5; 10), (20; 14), (7; 10), (7; 3)] 
7: [ (7; 8), (10; 22), (7; 3), (5; 10), (20; 14), (7; 10), (7; 3)] 
7: [ (7; 8), (10; 22), (7; 3), (5; 10), (20; 14), (7; 10), (7; 3)] 

7: [ (5; 10), (7; 3), (7; 3), (7; 8), (7; 10), (10; 22), (20; 14)] 
7: [ (5; 10), (7; 3), (7; 3), (7; 8), (7; 10), (10; 22), (20; 14)] 
7: [ (5; 10), (7; 3), (7; 3), (7; 8), (7; 10), (10; 22), (20; 14)] 

的數據是

type 
    Coord = Integer; 
    Coords1 = record X,Y: Coord; end; Coords2 = TPair< Coord, Coord >; 
    CoordsList1 = array of Coords1; CoordsList2 = TArray<Coords2>; 
    CoordsList3 = class (TList <Coords1>) public function ToString: string; override; end; 

var c1: CoordsList1; c2: CoordsList2; c3: CoordsList3; 

全部來源是在http://pastebin.ca/2294517 沒有 「嵌套如果」 比較功能,雖然可以如果你願意,可以提供一個。

相關問題