Search the Community
Showing results for tags 'tstinglist'.
Found 1 result
-
My TStringList custom SORTing, trying to mimic Windows Explorer way
programmerdelphi2k posted a topic in Algorithms, Data Structures and Class Design
Please don't be harsh with the comments... The principle is to identify the digits (numbers) contained in the text, add a bunch of "zeros" (to imitate the conversion into numerical values, but avoiding an "overflow" if using any text-to-number conversion function) and then represent them as their numerical value through the "ORD()" function. In this way, we avoid a possible "overflow exception", and we will be able to compare the strings (re-created for comparison purposes only) that are stored in a StringList or similar... I don't know if I managed to explain it well, but it needs testing... maybe in other languages. unit uMyTools; interface function MyNormalizeString(AStr: string; ASizeValue: byte = 10): string; function MyReCreatingMyString(AString: string): string; implementation uses System.SysUtils, System.StrUtils; const LMyDigits: TSysCharSet = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']; function MyNormalizeString(AStr: string; ASizeValue: byte = 10): string; var LStr: string; LVal: string; LEnd: integer; begin LStr := ''; LVal := ''; LEnd := AStr.Length; // if not(ASizeValue in [10 .. 20]) then ASizeValue := 10; // for var i: integer := 1 to LEnd do begin if CharInSet(AStr[i], LMyDigits) then begin LVal := LVal + AStr[i]; // if ((i + 1) <= LEnd) then begin if not(CharInSet(AStr[i + 1], LMyDigits)) then begin LStr := LStr + DupeString('0', ASizeValue - LVal.Length) + LVal; LVal := ''; end; end; end else begin LStr := LStr + AStr[i]; end; end; // if not LVal.IsEmpty then LVal := DupeString('0', ASizeValue - LVal.Length) + LVal; // result := LStr + LVal; end; function MyReCreatingMyString(AString: string): string; var LStr: string; begin result := ''; // LStr := MyNormalizeString(AString); // for var C in LStr do begin if CharInSet(C, LMyDigits) then result := result + ord(C).ToString else result := result + C; end; end; end. Testing.... 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 := MyReCreatingMyString(LCLeft); CmpRight := MyReCreatingMyString(LCRight); // result := CompareStr(CmpLeft, CmpRight); // if (result = 0) then result := CompareStr(LCLeft, LCRight); 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; // SL.Add('Delphi1World1Hello Windows'); // 1 space SL.Add('hello2'); SL.Add('hello10'); SL.Add('hello1'); SL.Add('hello4'); SL.Add('delphi 2'); // 2 spaces SL.Add('hello 000'); // 1 space SL.Add('delphi'); SL.Add('hello3'); SL.Add('Delphi3 World2023'); // 1 space SL.Add('Custom'); SL.Add('delphi 2'); // 1 space SL.Add('Delphi1.5World10 11'); // 1.5 - 1 space SL.Add('World'); SL.Add('Delphi 1'); // 1 space SL.Add('A B C'); // 1 space + 1 space SL.Add('hello000'); // 0 space SL.Add('abc'); SL.Add('delphi 2'); // 1 space SL.Add(''); // EMPTY!!! SL.Add('Delphi10'); SL.Add('Delphi1'); SL.Add('Delphi13'); SL.Add('Delphi1.5World10 21'); // 1.5 - 1 space SL.Add('Delphi001'); SL.Add('Delphi3'); SL.Add('Delphi3World2023'); SL.Add('Delphi3 Hi!'); // 1 space SL.Add('Delphi 5'); // 1 space SL.Add('Delphi1.2World1Hello Windows'); // 1 space SL.Add('Delphi2'); SL.Add('Delphi01'); SL.Add('Delphi 3World2023'); // 1 space SL.Add('Delphi 1'); // 1 space SL.Add('Delphi12'); SL.Add('Delphi4'); SL.Add('Delphi2.5World2022'); // 2.5 SL.Add('Hello3.5'); // SL.CustomSort(@MyStringListCustomSort); // Memo1.Lines.AddStrings(SL); finally SL.Free; end; end; initialization ReportMemoryLeaksOnShutdown := true; end.- 5 replies
-
- tstinglist
- sort
-
(and 2 more)
Tagged with: