Lazarus
Home
Help
TinyPortal
Search
Login
Register
Lazarus
»
Forum
»
Free Pascal
»
General
(Moderators:
FPK
,
Tomas Hajny
) »
[SOLVED] Help with TFPGMAP sort...
Free Pascal
Website
Downloads
Wiki
Documentation
Bugtracker
Mailing List
Lazarus
Website
Downloads (Laz+FPC)
Packages (OPM)
FAQ
Wiki
Documentation (RTL/FCL/LCL)
Bugtracker
CCR Bugs
GIT
Mailing List
Other languages
Foundation
Website
Useful Wiki Links
Project Roadmap
Getting the Source
Screenshots
How to use the forum
Forum Rules
About donations (wiki)
Bookstore
Computer Math and Games in Pascal
(preview)
Lazarus Handbook
Search
Advanced search
Recent
connecting to mariadb
by
alanyoung
[
Today
at 05:57:03 pm]
Why does the call via fun...
by
Remy Lebeau
[
Today
at 05:49:24 pm]
delete cookies
by
Leledumbo
[
Today
at 05:14:27 pm]
[hacked for now] Changing...
by
jamie
[
Today
at 04:40:15 pm]
Error: Enumeration symbol...
by
nanobit
[
Today
at 04:17:38 pm]
AI interactions
by
Zvoni
[
Today
at 02:19:48 pm]
TLazSerial : serial port ...
by
dbannon
[
Today
at 12:41:12 pm]
Recomendations for conver...
by
kompustelnik
[
Today
at 11:20:05 am]
Qt6 / X11: problem with G...
by
paweld
[
Today
at 11:14:43 am]
[Solved] Lookup field: er...
by
BlueIcaro
[
Today
at 09:59:57 am]
LCL Web Native with D2Bri...
by
egsuh
[
Today
at 08:10:14 am]
Access Violation - Databa...
by
Xenno
[
Today
at 06:21:52 am]
Question re. FCL's ssocke...
by
Curt Carpenter
[
Today
at 12:22:45 am]
SpkToolbar custom update
by
wp
[December 14, 2025, 11:55:55 pm]
MVP made easier.
by
cdbc
[December 14, 2025, 05:14:40 pm]
Lazarus is not working
by
Fred vS
[December 14, 2025, 05:14:20 pm]
TIBDataSet "Closing"? on ...
by
RedOctober
[December 14, 2025, 04:52:18 pm]
Running External Tools
by
JuhaManninen
[December 14, 2025, 04:08:28 pm]
The start of yet another ...
by
TBMan
[December 14, 2025, 03:16:21 pm]
Need help with Runcommand...
by
marcov
[December 14, 2025, 12:46:25 pm]
Military Grade Directives
by
Thaddy
[December 14, 2025, 11:46:44 am]
Cannot find Online Packag...
by
Thaddy
[December 14, 2025, 11:37:58 am]
Anyone interested in help...
by
ad1mt
[December 14, 2025, 08:56:20 am]
Hashing pointers by using...
by
440bx
[December 14, 2025, 03:16:33 am]
exception external SIGSEG...
by
dseligo
[December 14, 2025, 02:24:28 am]
« previous
next »
Print
Pages: [
1
]
Author
Topic: [SOLVED] Help with TFPGMAP sort... (Read 1680 times)
Espectr0
Full Member
Posts: 234
[SOLVED] Help with TFPGMAP sort...
«
on:
April 30, 2022, 11:07:47 pm »
Hello,
I have problems to order a TFPGMAP according to the DATA value:
type:
Code: Pascal
[Select]
[+]
[-]
TTMXList
=
specialize TFPGMap<
Integer
,
Double>
;
data compare:
Code: Pascal
[Select]
[+]
[-]
function
DataCompare
(
const
Data1
,
Data2
:
Double
)
:
Integer
;
begin
if
Data1 < Data2
then
Result
:
=
-
1
else
if
Data1 > Data2
then
Result
:
=
1
else
Result
:
=
0
;
end
;
Try the 'Sorted := True' property and 'Sort' but I can't get it to order.
What am I doing wrong?
thanks
«
Last Edit: May 01, 2022, 02:31:34 pm by Espectr0
»
Logged
bytebites
Hero Member
Posts: 767
Re: Help with TFPGMAP sort...
«
Reply #1 on:
May 01, 2022, 09:19:55 am »
Quote from: Espectr0 on April 30, 2022, 11:07:47 pm
...
What am I doing wrong?
You provided only code snippets.
Code: Pascal
[Select]
[+]
[-]
program
Project1
;
{$mode delphi}
uses
fgl
;
type
tmap
=
tfpgmap<
integer
,
double>
;
var
m
:
tmap
;
i
:
Integer
;
begin
m
:
=
tmap
.
create
;
m
.
Sorted
:
=
true
;
m
.
Add
(
1
,
1.1
)
;
m
.
Add
(
8
,
4.2
)
;
m
.
Add
(
2
,
2.3
)
;
m
.
Add
(
0
,
0.4
)
;
for
i
:
=
0
to
m
.
Count
-
1
do
writeln
(
m
.
Data
[
i
]
:
4
:
1
)
;
end
.
Output is
0.4
1.1
2.3
4.2
Logged
JdeHaan
Full Member
Posts: 170
Re: Help with TFPGMAP sort...
«
Reply #2 on:
May 01, 2022, 12:13:40 pm »
I believe sorting is done on the key only if you set sorted to true...
Logged
Espectr0
Full Member
Posts: 234
Re: Help with TFPGMAP sort...
«
Reply #3 on:
May 01, 2022, 01:17:09 pm »
Thanks @bytebites but @JdeHaan is right, the sort is based on the "KEY", so I tried "OnDataCompare" but I can't get it to work that way, any ideas?
Logged
Warfley
Hero Member
Posts: 2033
Re: Help with TFPGMAP sort...
«
Reply #4 on:
May 01, 2022, 02:11:00 pm »
Maps can only sort on the key, they are simply not designed to sort for data. Either use another datastructure or sort the data yourself.
I use this opportunity to plug my
iterator library
, which allows this:
Code: Pascal
[Select]
[+]
[-]
program
Project1
;
{$mode Delphi}
{$ModeSwitch implicitfunctionspecialization}
uses
Generics
.
Collections
,
iterators
;
type
TIntDoubleMap
=
TDictionary<
Integer
,
Double>
;
TIntDoublePair
=
TPair<
Integer
,
Double>
;
function
ComparePairValue
(
A
,
B
:
TIntDoublePair
)
:
Boolean
;
begin
Result
:
=
A
.
Value
< B
.
Value
;
end
;
var
data
:
TIntDoubleMap
;
p
:
TIntDoublePair
;
begin
data
:
=
TIntDoubleMap
.
Create
;
try
data
.
Add
(
1
,
5
)
;
data
.
Add
(
2
,
3
)
;
data
.
Add
(
4
,
6
)
;
data
.
Add
(
10
,
1
)
;
for
p
in
Sorted
(
Iterate
(
data
)
,
ComparePairValue
)
do
WriteLn
(
p
.
Value
)
;
finally
data
.
Free
;
end
;
ReadLn
;
end
.
«
Last Edit: May 01, 2022, 02:15:24 pm by Warfley
»
Logged
GitHub:
https://github.com/Warfley
Espectr0
Full Member
Posts: 234
[SOLVED] Help with TFPGMAP sort...
«
Reply #5 on:
May 01, 2022, 02:31:06 pm »
Thanks @Warfley, I'm going to investigate your library, I find it very interesting. Regards!
Logged
Print
Pages: [
1
]
« previous
next »
Lazarus
»
Forum
»
Free Pascal
»
General
(Moderators:
FPK
,
Tomas Hajny
) »
[SOLVED] Help with TFPGMAP sort...
TinyPortal
© 2005-2018