|
Ha ha, you have to learn to use it. Just change the comparison factor. As follows:
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; //Look at the result
B := StrToDateTime(SL[0]); //The most recent time
end;
My above is the absolute value of the difference. |
|