读书人

时间数组排序有关问题

发布时间: 2012-02-28 13:06:34 作者: rapoo

时间数组排序问题?
我现在有个数组,里面存放的是时间格式的数据,想把它按一定的顺序排列一下,以方便我找到自己想找的数据,怎么做啊?

有哪位知道帮个忙吧?我不胜感激!
在线等!

[解决办法]
呵呵,要活学活用.改变一下比较因子就可以了.如下:

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;

我上面是取的差异的绝对值.

读书人网 >.NET

热点推荐