求助:TList排序问题
问题是这样的,TList保存的对象有两个关键字需要排序,假设为v1,v2,均匀整型,采用Sort方法先对v2排序,再对v1排序,预期的效果是v1相同的情况下v2是排序的,但是经实测v2是无序的,请问有什么办法可以实现我的预期效果。
程序代码:
type TBat = record v1: Integer; v2: Integer; v3: Integer; end; PBat = ^TBat; function SortValue(p1,p2: PBat): Integer; begin result:=CompareValue(p2.v1,p1.v1); if result = 0 then result:=CompareValue(p2.v2,p1.v2); if result = 0 then result:=CompareValue(p2.v3,p1.v3); end; procedure TForm1.FormClick(Sender: TObject); var i,j: Integer; list: TList; bat: PBat; begin list:=TList.Create; try randomize; for i:=0 to 2 do for j:=0 to 3 do begin new(bat); bat.v1:=i*10; bat.v2:=(j div 2)*20; bat.v3:=random(20); list.Add(bat); end; list.Sort(@SortValue); self.Repaint; for i:=0 to list.Count-1 do begin bat:=list[i]; canvas.TextOut(20, 20*(i+1), inttostr(bat.v1)); canvas.TextOut(50, 20*(i+1), inttostr(bat.v2)); canvas.TextOut(80, 20*(i+1), inttostr(bat.v3)); end; finally for i:=0 to list.Count-1 do dispose(list[i]); list.Free; end; end;
[此贴子已经被作者于2016-3-7 21:18编辑过]