1

我在 ClientDataSet 中有 3 条记录的结构:

数字:整数;开始:TTime;结束:TTime;

1个;9:00;10:00
2;9点30分;10:00
3;9点40分;10:20

我必须像这样将这些记录传递给其他 ClientDataSet:

1个;9:00;9:30
2:9:30;9:40
3;9点40分;10:00
4;10:00;10:20
4

1 回答 1

5

使用TList<TTime>,用两列填充它,对其进行排序,然后遍历列表跳过重复项。像这样。

var l: TList<TTime>; t1, t2: TTime; id2: cardinal;
begin
  l := TList<TTime>.Create;
  try
    cds1.First;
    while not cds1.Eof do begin
       l.Add( Frac(cds1.Fields[1].AsDateTime) );
       l.Add( Frac(cds1.Fields[2].AsDateTime) );
       cds1.Next;   
    end;

    l.Sort;

    cds2.Clear;
    if l.Count <= 0 then exit;

    t1 := l[0]; id2 := 0;
    for t2 in l do begin
        if t1 > t2 then raise Exception.create ('Sort silently failed!') else
        if t1 < t2 then begin
           Inc(id2);
           cds2.AppendRecord([ id2, t1, t2 ]);
           t1 := t2;
        end;
    end;
  finally 
    l.Free;
  end;
end;
于 2013-02-26T14:56:52.197 回答