Skip to content

Instantly share code, notes, and snippets.

@mk-pmb
Created November 11, 2016 13:13
Show Gist options
  • Save mk-pmb/9fa29ae3b545ae9a9f47d29b63f8b033 to your computer and use it in GitHub Desktop.
Save mk-pmb/9fa29ae3b545ae9a9f47d29b63f8b033 to your computer and use it in GitHub Desktop.
Sort TStrings (TMemo.Lines) in Lazarus-IDE
procedure SortStrings(strs: TStrings);
var
sl: TStringList;
begin
sl := TStringList.Create;
sl.Text := strs.Text;
sl.Sort;
strs.Text := sl.Text;
sl.Free;
end;
Freenode IRC #lazarus-ide 2016-11-11
<Sven_vB> hi :)
<Sven_vB> what's the easiest way to sort the lines in my TMemo? I think I've
used Memo.Lines.Sort or Memo.Sorted := True back in the days but it seems
to not work nowadays.
<Sven_vB> oh I see, I have to remind the lines that they're TStringList
<Sven_vB> nope, that makes an "Invalid type cast."
<Sven_vB> there's "TStrings.Sorted" in the "See also" section of
http://lazarus-ccr.sourceforge.net/docs/rtl/classes/tstringlist.html ,
but when I follow the link, Firefox can't find any occurrence of "sort"
in the target page. :-/
<Sven_vB> that TypeCast won't even work when I replace the Lines with a real
TStringList?? EdtVars.Lines.Free; EdtVars.Lines := TStringList.Create;
(EdtVars.Lines as TStringList).Sort;
<Sven_vB> seems I have to make my own helper proc where I copy the .Text into
a temporary TStringList, sort it, and copy it back. :-( cumbersome but works.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment