Guest User

Untitled

a guest
May 1st, 2020
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 3.27 KB | None | 0 0
  1. //All in one:
  2. function ScriptCoreQuckSortAllIn(List: TStringList; Separator: string; SortingDirection:boolean): TStringList;
  3. var i,b: int64; TempLen, NumCounterMax: byte; ForTest: TStringList; TempString: String;
  4. begin
  5.     if List.Count > 0 then begin
  6.         ForTest := File.CreateStringList();
  7.         for i := 0 to List.Count-1 do begin
  8.             ForTest.Append(GetPieceSC3(List[i],Separator,0));
  9.             TempLen := Length(ForTest[i]);
  10.             if TempLen > NumCounterMax then NumCounterMax := TempLen;
  11.         end;
  12.         for i := 0 to List.Count-1 do begin
  13.             TempString := ForTest[i];
  14.             for b:= 1 to NumCounterMax - length(ForTest[i]) do TempString := '0' + TempString;
  15.             List[i] := TempString+Copy(List[i],Length(ForTest[i]+Separator),Length(List[i]));
  16.         end;
  17.         ForTest.Free;
  18.         With List do
  19.             try
  20.                 BeginUpdate;
  21.                 List.Sort;
  22.                 if SortingDirection then begin
  23.                     for i := 0 to List.Count-1 do begin
  24.                         TempLen := Length(List[i]);
  25.                         for b := 1 to TempLen do begin
  26.                             TempString := List[i];
  27.                             if TempString[b] = '0' then continue else
  28.                             begin
  29.                                 List[i] := Copy(TempString,b,TempLen);
  30.                                 break;
  31.                             end;
  32.                         end;
  33.                     end;
  34.                 end else
  35.                 begin
  36.                     for i := 0 to List.Count-1 do begin
  37.                         TempLen := Length(List[i]);
  38.                         for b := 1 to TempLen do begin
  39.                             TempString := List[i];
  40.                             if TempString[b] = '0' then continue else
  41.                             begin
  42.                                 List[i] := Copy(TempString,b,TempLen);
  43.                                 break;
  44.                             end;
  45.                         end;
  46.                         List.Move(i, 0);
  47.                     end;
  48.                 end;
  49.                 Result := List;
  50.             Finally EndUpdate
  51.         end;
  52.     end;
  53. end;
  54.  
  55. //or separated auxiliary functions:
  56.  
  57. function GetPieceSC3(Str, Reg: string; Number: Word): string;
  58. var Res: TStringList;
  59. begin
  60.     try
  61.         Res := File.CreateStringList;
  62.         SplitRegExpr(QuoteRegExprMetaChars(Reg), Str, Res);
  63.         Result := Res.Strings[Number];
  64.     except
  65.         Result := '';
  66.     finally
  67.         Res.Free;
  68.     end;
  69. end;
  70.  
  71. function ZeroFill(S: string; Peak: integer; IsEnabled: boolean): string;
  72. var i, m: integer;
  73. begin
  74.     if IsEnabled then begin
  75.         m := Peak - length(S);
  76.         for i:= 1 to m do S := '0' + S;
  77.     end;
  78.     result := S;
  79. end;
  80.  
  81. function ZeroRemover(s:string):string;
  82. var i, TempLen: integer;
  83. begin
  84.     TempLen := Length(s);
  85.     for i := 1 to TempLen do begin
  86.         if s[i] = '0' then continue else begin
  87.             Result := Copy(s,i,TempLen);
  88.             exit;
  89.         end;
  90.     end;
  91. end;
  92.  
  93. function ScriptCoreQuckSort(List: TStringList; Separator: string; SortingDirection:boolean): TStringList;
  94. var i: int64; TempLen, NumCounterMax: byte; ForTest: TStringList;
  95. begin
  96.     if List.Count > 0 then begin
  97.         ForTest := File.CreateStringList();
  98.         for i := 0 to List.Count-1 do begin
  99.  
  100.             ForTest.Append(GetPieceSC32(List[i],Separator,0));
  101.             TempLen := Length(ForTest[i]);
  102.             if TempLen > NumCounterMax then NumCounterMax := TempLen;
  103.         end;
  104.         for i := 0 to List.Count-1 do List[i] := ZeroFill2(ForTest[i],NumCounterMax,true)+Copy(List[i],Length(ForTest[i]+Separator),Length(List[i]));
  105.         ForTest.Free;
  106.         With List do
  107.             try
  108.                 BeginUpdate;
  109.                 List.Sort;
  110.                 if SortingDirection then for i := 0 to List.Count-1 do List[i] := ZeroRemover(List[i]) else
  111.                 begin
  112.                     for i := 0 to List.Count-1 do begin
  113.                         List[i] := ZeroRemover(List[i]);
  114.                         List.Move(i, 0);
  115.                     end;
  116.                 end;
  117.                 Result:=List;
  118.             Finally EndUpdate
  119.         end;
  120.     end;
  121. end;
Add Comment
Please, Sign In to add comment