I think the simplest way would be something along these lines:
- Transfer the content from the memo to a
TStringList
instance.
- Call
CustomSort
on the TStringList
instance, passing an appropriate sort compare function.
- Transfer the content back to the memo.
Steps 1 and 3 are simple calls to Assign
. So step 1 would be:
StringList.Assign(Memo.Lines);
And step 3 would be:
Memo.Lines.Assign(StringList);
Step 2 is the tricky bit. You've got to provide a compare function of this type:
TStringListSortCompare = function(List: TStringList;
Index1, Index2: Integer): Integer;
Your function will look like this:
function MySortCompare(List: TStringList; Index1, Index2: Integer): Integer;
begin
Result := MyCompareStrings(List[Index1], List[Index2]);
end;
where MyCompareStrings
is a function that compares two strings according to your rules. The return value of that function follows the usual convention for a compare function. Negative means less than, positive means greater than and zero means equal.
Of course, you can write the logic directly inline to MySortCompare
if you prefer.