Author Topic: procedure Sort_TStringList_By_Values()  (Read 99 times)

0 Members and 1 Guest are viewing this topic.

Offline soldat-game

  • Camper
  • ***
  • Posts: 402
procedure Sort_TStringList_By_Values()
« on: September 09, 2022, 10:30:29 am »
Procedure name: Sort_TStringList_By_Values
Script description: The procedure will let you sort the tstringlist string using values. It allows you to sort your lists very quickly and efficiently by "values".
Version: 1.0
Author: dominikk26
Compile test: Passed
Core version: 2.8.2+
Download: Attachment! Click here
Full description: Unfortunately, script core does not allow you to use the full potential of pascal.
And alternative methods are inefficient. That's why I created a library. This is a very simple code that you will find below. Test on windows 11 and linux debian 11.
Code: [Select]
library Sort_By_Values;

{$mode objfpc}{$H+}

uses
  SysUtils, Classes;

function StringListSortProc(List: TStringList; Index1, Index2: Integer): Integer;
var i1, i2: Integer;
begin
i1 := StrToInt(List.ValueFromIndex[Index1]);
i2 := StrToInt(List.ValueFromIndex[Index2]);
Result := i2 - i1;
end;

procedure Sort_TStringList_By_Values(Where: string);
var Data_To_Sort: TStringList;
begin
Data_To_Sort := TStringList.Create;
try
Data_To_Sort.LoadFromFile(Where);
Data_To_Sort.CustomSort(@StringListSortProc);
Data_To_Sort.SaveToFile(Where);
finally
Data_To_Sort.Free;
end;
end;

exports
Sort_TStringList_By_Values;

begin
end.

How use? Example:
Code: [Select]
interface

procedure Sort_TStringList_By_Values(Where: string);
external {$IFDEF WIN32} 'Sort_TStringList_By_Values@sort.dll'      {$ELSE} 'Sort_TStringList_By_Values@sort.so'      {$ENDIF};

begin

procedure Sort_Rank_By_Values();
begin
Rank_List.SaveToFile(Script.Dir+'/data/temp');
Rank_List.Free;
Sort_TStringList_By_Values(Script.Dir+'/data/temp');
Rank_List := File.CreateStringListFromFile(Script.Dir+'/data/temp');
File.Delete(Script.Dir+'/data/temp');
end;

end.
« Last Edit: September 09, 2022, 10:35:46 am by soldat-game »