Get access to over 100 FireMonkey cross platform samples for Android, IOS, OSX, Windows, and Linux!

AndroidAppmethodCode SnippetDelphiDemoFiremonkeyIOSOSXWindows

Easily Sort String Lists And Objects With CustomSort In Delphi XE7 Firemonkey On Android And IOS

| Delphi 11 10 XE8 XE7 XE Seattle Berlin Tokyo Rio Firemonkey Delphi Android Delphi IOS

Delphi XE7 Firemonkey Custom Sort String Object List | Delphi 11 10 XE8 XE7 XE Seattle Berlin Tokyo Rio Firemonkey Delphi Android Delphi IOSIt 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.

Have Delphi Firemonkey questions? Ask and get answers on StackOverflow.

Related posts
DelphiDemoFiremonkeyLinuxOSXShowcaseWindows

AutoBlogAI: FireMonkey Client To Leverage LLMs And Generative AI For Blogging

DelphiFiremonkeyShowcaseUtilityWindows

Unleashing Creativity With Song Writer AI: A Deep Dive

DelphiFiremonkeyShowcaseWindows

How To Build Stable Diffusion Text To Image Prompts

AndroidC++BuilderDelphiFiremonkeyIOSOSXWindows

FireMonkey 10.4.2 Features Updated iOS 14, Android 11, And macOS 11 Support Plus Hundreds Of Fixes

Sign up for our Newsletter and
stay informed

Leave a Reply