2012-12-17 2 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 에있다있는 것 원할 경우 하나를 제공하십시오.

관련 문제