function Compare(List: TStringList; Index1, Index2: Integer): Integer; begin Result := CompareText(List[Index1], List[Index2]); //升序 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[5]); end;
呵呵,要活学活用.改变一下比较因子就可以了.如下:const Point = '2006-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;我上面是取的差异的绝对值.
a: array[0..1024] of tdatetime;
i: integer;
b: tdatetime;
begin
a[i] := datetimepicker1.datetime; //加多个时间 {对数组进行排序} b := a[i]; //这就是要的结果
end;
begin
Result := CompareText(List[Index1], List[Index2]); //升序
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[5]);
end;
这样的:
stringlist里面的那个最大的小于指定数的数怎么找,没怎么用过stringlist,你再指点一下!!
万分感谢!
Point = '2006-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;我上面是取的差异的绝对值.