Lazarus

Programming => General => Topic started by: mangakissa on July 09, 2020, 12:34:19 pm

Title: Sorting TStringArray with keys from TFPGMap
Post by: mangakissa on July 09, 2020, 12:34:19 pm
In Delphi I use this line of code to sort a TDictionary
Code: Pascal  [Select][+][-]
  1. var    sortedDictKeys : TList<string>;
  2. begin
  3.   sortedDictKeys := TList<string>.Create(flink.CellInhoud.Keys); //TDictionary
  4.   sortedDictKeys.Sort;
  5. end;
  6.  
I converted my project to lazarus. This is my converted code:
Code: Pascal  [Select][+][-]
  1. var    sortedDictKeys : TStringArray;
  2. begin
  3.   sortedDictKeys := TStringArray.Create(flink.CellInhoud.Keys); //TFPGMap
  4.   sortedDictKeys.Sort;
  5. end;
  6.  
flink.CellInhoud is a TFPGMap structure. I want to get all keyvalues to TStringarray, but keys needs a parameter to read. How do I read all keys at once.

TStringArray doesn't sort.
Which option do I need to sort a single line of array in my case?

I don't want to sort TFPGMap, because it not sorting on name, but on pointer.

Lazarus 2.06 / FPC 3.04
Title: Re: Sorting TStringArray with keys from TFPGMap
Post by: PascalDragon on July 09, 2020, 01:21:32 pm
You can use Generics.Collections in FPC as well. For 3.0.4 you can download it here (https://github.com/maciej-izak/generics.collections), for 3.2 and newer its integrated into the distribution.
Title: Re: Sorting TStringArray with keys from TFPGMap
Post by: mangakissa on July 09, 2020, 01:50:43 pm
Yes, I know. But is there also a solution for fgl?
Title: Re: Sorting TStringArray with keys from TFPGMap
Post by: PascalDragon on July 09, 2020, 04:35:45 pm
You need to assign a OnKeyCompare (https://freepascal.org/docs-html/current/rtl/fgl/tfpgmap.onkeycompare.html) event handler.
TinyPortal © 2005-2018