It is simple to sort a list of strings or objects in Object Pascal using TStringList from Delphi XE7 Firemonkey. TStringList provides a CustomSort function which you pass a function to that handles the sorting. The function that you pass to CustomSort should have three parameters and look like this StringListCompareStrings(List: TStringList; Index1, Index2: Integer): Integer;. The Index1 and Index2 parameters are the row IDs from the TStringList. You can do whatever you want with the row IDs. For example, you can just use the row data directly or you could access an object attached to that row and pull a specific piece of data off the object for sorting. This custom sorting function works on Android, IOS, OSX, and Windows. It should also compile under Appmethod. Here is a code snippet which demonstrates the CustomSort functionality to sort the rows as integers:
function StringListCompareStrings(List: TStringList; Index1, Index2: Integer): Integer;
var
First: Integer;
Second: Integer;
begin
First := StrToInt(List[Index1]);
Second := StrToInt(List[Index2]);
if First>Second then
Result := 1
else if First=Second then
Result := 0
else if First<Second then
Result := -1;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
SL: TStringList;
begin
SL := TStringlist.Create;
SL.Append('3');
SL.Append('4');
SL.Append('1');
SL.Append('2');
SL.Append('5');
SL.CustomSort(StringListCompareStrings);
Memo1.Lines.AddStrings(SL);
SL.Free;
end;
Download the demo source code for creating a custom sort function for TStringList in Delphi XE7 Firemonkey.