我现在有个数组,里面存放的是时间格式的数据,想把它按一定的顺序排列一下,以方便我找到自己想找的数据,怎么做啊?有哪位知道帮个忙吧?我不胜感激!
在线等!

解决方案 »

  1.   

    var
      a: array[0..1024] of tdatetime;
      i: integer;
      b: tdatetime;
    begin
      a[i] := datetimepicker1.datetime; //加多个时间  {对数组进行排序}  b := a[i];  //这就是要的结果
    end;
      

  2.   

    你把时间格式化(FormatDateTime('YYYY-MM-DD HH:MMMM:SS', A[I]))加入到一个TStringList里面,根据你的要求写定排序函数因子(如升、降序),然后用CustomSort排序。 在取出数据时再把字符串格式化成TDateTime(B[I] := StrToDateTime(SL[I]))
      

  3.   

    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;
      

  4.   

    老冯,还是不行,时间数据是排序了,但是我去没法把我要的那个数据找出来啊!
    这样的:
    stringlist里面的那个最大的小于指定数的数怎么找,没怎么用过stringlist,你再指点一下!!
    万分感谢!
      

  5.   

    呵呵,要活学活用.改变一下比较因子就可以了.如下: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;我上面是取的差异的绝对值.