TstringList有Sort方法和CustomSort方法,sort用来按字母顺序升序排列,现在如果我要将某个TstringList里面的内容按降序排列,应该如何做?

解决方案 »

  1.   

    var
      SL: TStringList;function StringListCompareStrings(List: TStringList; Index1, Index2: Integer): Integer;
    begin
      if SL.CaseSensitive then
        Result := AnsiCompareStr(List[Index2], List[Index1])
      else
        Result := AnsiCompareText(List[Index2], List[Index1]);
    end;procedure TForm1.Button2Click(Sender: TObject);
    begin
      SL := TStringList.Create;
      SL.Assign(Memo1.Lines);
      SL.CustomSort(StringListCompareStrings);
      Memo1.Lines.Assign(SL);
      SL.Free;
    end;
      

  2.   

    再放一个TStringList
    sl2
    ......
    sl1.clear;
    for i:=sl2.Count-1 downto 0 do
      sl1.items.add(sl2.items[i]);