TOBJECTLIST里,有自带的排序功能
TLIST,TSTRINGLIST也有,MS是一样的
SORT里有一个参数: Compare:TListSortCompare
那我们先了解一下 TListSortCompare type
HELP原文:TListSortCompare is the type for callbacks that compare two items in a list.
即 TListSortCompare 是一个比较两个列表项的的回调类型
不知道这么翻译行不行,但意思就是返回一个值来表示LIST中两项的大小
定义:(这个传入的是两项的指针)
type TListSortCompare = function (Item1, Item2: Pointer): Integer;
Value Description
> 0 (positive) Item1 is less than Item2
0 Item1 is equal to Item2
< 0 (negative) Item1 is greater than Item2
于是我们可以定义一个比较函数去定义自己的比较方式
function (Item1, Item2: Pointer): Integer
比如:
function CompareProgressLine(Item1, Item2: Pointer): Integer;
var
nValue1, nValue2: Integer;
nRatio1, nRatio2: Double;
begin
// 比较车辆的在线数量
// 从大到小排列
nValue1 := TProgressLine(Item2).GetVehicleNumOnLine();
nValue2 := TProgressLine(Item1).GetVehicleNumOnLine();
if nValue1 <> nValue2 then
Result := Integer(CompareValue(nValue2, nValue1))
else
begin
nRatio1 := TProgressLine(Item1).NProjectProductAchieve / TProgressLine(Item1).NProjectProductExpect;
nRatio2 := TProgressLine(Item2).NProjectProductAchieve / TProgressLine(Item2).NProjectProductExpect;
// 比较哪个更接近完成,比值越大,越接近完成
Result := Integer(CompareValue(nRatio1, nRatio2));
end;
end;
但要注意的是
如果你用的是TLIST要对Item1,Item2取地址原值,即Item1^
如果用的是TOBJECTLIST,那么就不用取地址原值