DELPHI盒子
!实时搜索: 盒子论坛 | 注册用户 | 修改信息 | 退出
检举帖 | 全文检索 | 关闭广告 | 捐赠
技术论坛
 用户名
 密  码
自动登陆(30天有效)
忘了密码
≡技术区≡
DELPHI技术
lazarus/fpc/Free Pascal
移动应用开发
Web应用开发
数据库专区
报表专区
网络通讯
开源项目
论坛精华贴
≡发布区≡
发布代码
发布控件
文档资料
经典工具
≡事务区≡
网站意见
盒子之家
招聘应聘
信息交换
论坛信息
最新加入: sy1012
今日帖子: 0
在线用户: 7
导航: 论坛 -> DELPHI技术 斑竹:liumazi,sephil  
作者:
男 emailx45 (emailx45) ▲▲▲▲△ -
普通会员
2023/6/23 6:51:26
标题:
How SORT a StringList text with numbers as MSWindows Explorer file order by name: by Emailx45 浏览:1011
加入我的收藏
楼主: hello boys and girls,

here try to mimic MSWindows Explorer order file names by name
-- not 100% like MSWindows but works for many cases!


------- my uMyTools.pas
unit uMyTools;

interface

function MyNormalizeString(AStr: string; AValLength: byte = 10): string;

implementation

uses
  System.SysUtils,
  System.StrUtils;

const
  LMyDigits: TSysCharSet = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];

function MyNormalizeString(AStr: string; AValLength: byte = 10): string;
var
  LStr: string;
  LVal: string;
  LEnd: integer;
begin
  LStr := '';
  LVal := '';
  LEnd := AStr.Length;
  //
  { ex. Text1 and Text1234567890 and Text123456789012345 = part complex!!!  AValLength := ?
    Text0000000001
    Text1234567890
    Text123456789012345
    //
    ... we can have it with distinct length, then let's use an arbitrary value!
  }
  if (AValLength < 10) then
    AValLength := 10
  else
    if (AValLength > 20) then
      AValLength := 20;
  //
  for var i: integer := 1 to LEnd do
    begin
      if CharInSet(AStr[i], LMyDigits) then
        begin
          LVal := LVal + AStr[i];
          //
          if ((i + 1) <= LEnd) and not(CharInSet(AStr[i + 1], LMyDigits)) then
          begin
          LStr := LStr + DupeString('0', AValLength - LVal.Length) + LVal;
          LVal := '';
          end;
        end
      else
        LStr := LStr + AStr[i];
    end;
  //
  if not LVal.IsEmpty then
    LVal := DupeString('0', AValLength - LVal.Length) + LVal;
  //
  result := LStr + LVal;
end;

end.

------- my Form1 tests:
//...

type
  TForm1 = class(TForm)
    Btn_CustomSort: TButton;
    Memo1: TMemo;
    Btn_NO_ORDer: TButton;
    procedure Btn_CustomSortClick(Sender: TObject);
    procedure Btn_NO_ORDerClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

uses
  uMyTools;

function MyStringListCustomSort(SL: TStringList; ALeft, ARight: integer): integer;
var
  LCLeft, LCRight  : string;
  CmpLeft, CmpRight: string;
begin
  LCLeft  := LowerCase(SL[ALeft]);
  LCRight := LowerCase(SL[ARight]);
  //
  CmpLeft  := MyNormalizeString(LCLeft);
  CmpRight := MyNormalizeString(LCRight);
  //
  result := CompareStr(CmpLeft, CmpRight);
  //
  if (result = 0) then
    result := CompareStr(LCLeft, LCRight);
end;

procedure MyStringListADDs(var ASL: TStringList);
begin
  ASL.Add('Delphi1World1Hello Windows'); // 1 space
  ASL.Add('hello2');
  ASL.Add('hello10');
  ASL.Add('hello1');
  ASL.Add('hello4');
  ASL.Add('delphi  2'); // 2 spaces
  ASL.Add('hello 000'); // 1 space
  ASL.Add('delphi');
  ASL.Add('hello3');
  ASL.Add('Delphi3 World2023'); // 1 space
  ASL.Add('Custom');
  ASL.Add('delphi 2');          // 1 space
  ASL.Add('Delphi1.5World10 11'); // 1.5 - 1 space
  ASL.Add('World');
  ASL.Add('Delphi 1'); // 1 space
  ASL.Add('A B C');    // 1 space + 1 space
  ASL.Add('hello000'); // 0 space
  ASL.Add('abc');
  ASL.Add('delphi 2'); // 1 space
  ASL.Add('');         // EMPTY!!!
  ASL.Add('Delphi10');
  ASL.Add('Delphi1');
  ASL.Add('Delphi13');
  ASL.Add('Delphi1.5World10 21'); // 1.5 - 1 space
  ASL.Add('Delphi001');
  ASL.Add('Delphi3');
  ASL.Add('Delphi3World2023');
  ASL.Add('Delphi3 Hi!');          // 1 space
  ASL.Add('Delphi 5');          // 1 space
  ASL.Add('Delphi1.2World1Hello Windows'); // 1 space
  ASL.Add('Delphi2');
  ASL.Add('Delphi01');
  ASL.Add('Delphi 3World2023'); // 1 space
  ASL.Add('Delphi 1');          // 1 space
  ASL.Add('Delphi12');
  ASL.Add('Delphi4');
  ASL.Add('Delphi2.5World2022'); // 2.5
  ASL.Add('Hello3.5');
end;

procedure TForm1.Btn_CustomSortClick(Sender: TObject);
var
  SL: TStringList;
begin
  Memo1.Lines.Clear;
  //
  SL := TStringList.Create;
  try
    SL.Sorted     := false;
    SL.Duplicates := TDuplicates.dupAccept;
    //
    MyStringListADDs(SL);
    //
    SL.CustomSort(@MyStringListCustomSort);
    //
    Memo1.Lines.AddStrings(SL);
  finally
    SL.Free;
  end;
end;

procedure TForm1.Btn_NO_ORDerClick(Sender: TObject);
var
  SL: TStringList;
begin
  Memo1.Lines.Clear;
  //
  SL := TStringList.Create;
  try
    SL.Sorted     := false;
    SL.Duplicates := TDuplicates.dupAccept;
    //
    MyStringListADDs(SL);
    //
    Memo1.Lines.AddStrings(SL);
  finally
    SL.Free;
  end;
end;

initialization

ReportMemoryLeaksOnShutdown := true;

end.
此帖子包含附件:
GIF 图像
大小:206.8K
----------------------------------------------
The higher the degree, the greater the respect given to the humblest!RAD 11.3
作者:
男 emailx45 (emailx45) ▲▲▲▲△ -
普通会员
2023/6/24 2:55:35
1楼: no needs use MyReCreatingMyString(...) function

--- It can be DELETEd!
----------------------------------------------
The higher the degree, the greater the respect given to the humblest!RAD 11.3
作者:
男 think1994 (小白) ▲△△△△ -
普通会员
2023/6/27 16:00:49
2楼: 可以使用Windows系统自带的StrCmpLogicalW 字符串逻辑比较函数,字符串中的数字会根据数字顺序进行排序。
函数定义:function StrCmpLogicalW( pszl, psz2: PWideChar ):Integer; stdcall; external 'shlwapi.dll';
----------------------------------------------
-
作者:
男 emailx45 (emailx45) ▲▲▲▲△ -
普通会员
2023/6/28 0:01:10
3楼: @think1994

using StrCmpLogicalW + ShlWApi.dll  = high dependence on MSWindows platform!
--- NOT cross-platform!

my simple sample is cross-platform = no dependences!
----------------------------------------------
The higher the degree, the greater the respect given to the humblest!RAD 11.3
作者:
男 think1994 (小白) ▲△△△△ -
普通会员
2023/6/28 10:16:47
4楼: 是的,你说的有道理,我没有考虑平台无关性问题
----------------------------------------------
-
作者:
男 emailx45 (emailx45) ▲▲▲▲△ -
普通会员
2023/6/28 22:24:50
5楼: in my tests, the result is the same between my-code...StrCmpLogicalW

... of course, needs more "intensity" on tests...
----------------------------------------------
The higher the degree, the greater the respect given to the humblest!RAD 11.3
作者:
男 truelsm ( ) ★☆☆☆☆ -
盒子活跃会员
2024/5/8 14:05:14
6楼: 楼主厉害,速度非常棒,完美。
----------------------------------------------
-
信息
登陆以后才能回复
Copyright © 2CCC.Com 盒子论坛 v3.0.1 版权所有 页面执行80.07813毫秒 RSS