|
发表于 2020-7-11 16:15:01
|
显示全部楼层
呵呵,要活学活用.改变一下比较因子就可以了.如下:
const
Point = '2016-1-6 12:00:00';
function Compare(List: TStringList; Index1, Index2: Integer): Integer;
var
Diff1, Diff2: Double;
begin
Diff1 := ABS(StrToDateTime(List[Index1]) - StrToDateTime(Point));
Diff2 := ABS(StrToDateTime(List[Index2]) - StrToDateTime(Point));
if Diff1 > Diff2 then
Result := 1;
if Diff1 = Diff2 then
Result := 0;
if Diff1 < Diff2 then
Result := -1;
end;
procedure TFormDemo.ButtonDemoClick(Sender: TObject);
var
A: array[0..1024] of TDateTime;
I: Integer;
B: TDateTime;
SL: TStringList;
begin
Randomize;
SL := TStringList.Create;
for I := 0 to 1024 do
begin
A[I] := DateUtils.IncHour(Now, Random(24));
SL.Add(FormatDateTime('YYYY-MM-DD HH:MMMM:SS', A[I]));
end;
SL.CustomSort(@Compare);
ListBox.Items := SL; //看看结果
B := StrToDateTime(SL[0]); //最近的时间
end;
我上面是取的差异的绝对值.
|
|