惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

IT之家
IT之家
Y
Y Combinator Blog
月光博客
月光博客
Blog — PlanetScale
Blog — PlanetScale
GbyAI
GbyAI
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
博客园 - 三生石上(FineUI控件)
S
SegmentFault 最新的问题
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
美团技术团队
雷峰网
雷峰网
酷 壳 – CoolShell
酷 壳 – CoolShell
Last Week in AI
Last Week in AI
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
有赞技术团队
有赞技术团队
博客园 - 司徒正美
V
Visual Studio Blog
小众软件
小众软件
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
T
Tailwind CSS Blog
Apple Machine Learning Research
Apple Machine Learning Research
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
A
About on SuperTechFans
The Cloudflare Blog

博客园 - shappy

5.12默哀 台风 - shappy - 博客园 童谣 回家 Web Control中填写JavaScript报告"缺少对象"错误问题解决 Midas如何在服务器端自动产生流水号 Clientdataset关于record change by another user错误的总结 游凤凰 设定MSAgent的说话(Balloon)停留时间 获取Agent角色的动作列表 MSAgent简介 摘录 DelphiX简介 摘录 DelphiX的刷新 TList源码分析 摘录 几种文字编码的介绍 获取/打开/关闭输入法 汉字编码标准与识别 摘录 关于Tclientdataset的bug 队列类
Tclientdataset实现反向排序
shappy · 2007-05-14 · via 博客园 - shappy

ADO的sort可以很方便实现正向和方向排序,clt的排序可以用indexfieldnames,和indexDef.但是只支持正向排序.测试很长时间都没有办法利用这两个属性来实现反向排序,查看了clt的代码后,通过修改实现:

procedure TClientDataSet.SortOnFields(Cursor: IDSCursor; const Fields: string;
  CaseInsensitive, Descending: Boolean);
var
  I: Integer;
  FieldList: TList;
  DescFlags, CaseFlags: DSKEYBOOL;
  //shappy
  sFlds:string;

  function GetFlags(Flag: Bool; var FlagArray: DSKEYBOOL): Pointer;
  var
    J: Integer;
    //shappy
    s,s2:string;
    i:Integer;
  begin
{    if not Flag then Result := nil else
    begin
      for J := 0 to FieldList.Count - 1 do
        FlagArray[J] := True;
      Result := @FlagArray;
    end;}
    //shappy 修改成类似ado.sort的效果,可能存在bug,效率无优化
    if not Flag then begin
      s:= sFlds;
      i:=pos(';',s);  j:=0;
      while i>0 do begin
        if Copy(s,i-5,5)=' DESC' then
          FlagArray[j]:=True
        else
          FlagArray[j]:=False;
        inc(j);
        s:=copy(s,i+1,MaxInt);
        i:=pos(';',s);
      end;
      if (s<>'') and (Copy(s,Length(s)-4,5)=' DESC') then
        FlagArray[j]:=True
      else FlagArray[j]:=False;
    end else begin
      for J := 0 to FieldList.Count - 1 do
        FlagArray[J] := True;
      Result := @FlagArray;
    end;
  end;

begin
  FieldList := TList.Create;
  try
    //shappy
    sFlds:=UpperCase(Fields);
    GetFlags(Descending, DescFlags);
    i:=pos(' DESC',sFlds);
    while i>0 do begin
      sFlds:=Copy(sFlds,1,i-1)+copy(sFlds,i+5,MaxInt);
      i:=pos(' DESC',sFlds);
    end;
    GetFieldList(FieldList, sFlds);

    //GetFieldList(FieldList, Fields);
    for I := 0 to FieldList.Count - 1 do
      if TField(FieldList[I]).FieldNo > 0 then
        FieldList[I] := Pointer(TField(FieldList[I]).FieldNo) else
        DatabaseError(SFieldIndexError, Self);
      //shappy
      Check(Cursor.SortOnFields(FieldList.Count, PDWord(FieldList.List),
        @DescFlags, GetFlags(CaseInsensitive, CaseFlags)));
//    Check(Cursor.SortOnFields(FieldList.Count, PDWord(FieldList.List),
//      GetFlags(Descending, DescFlags), GetFlags(CaseInsensitive, CaseFlags)));
    GetIndexInfo('');
  finally
    FieldList.Free;
  end;
end;