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
Questions on slight diffe...
by
egsuh
[
Today
at 09:45:30 am]
Have anybody used Horse f...
by
egsuh
[
Today
at 08:31:21 am]
UI app to work with SQLit...
by
paweld
[
Today
at 07:18:23 am]
TCollection wiki entry
by
Thaddy
[
Today
at 07:06:53 am]
is there PDS reader/expor...
by
Weiss
[
Today
at 06:55:58 am]
equivalent to C/C++ "offs...
by
Fibonacci
[
Today
at 02:44:35 am]
Printing on HP Smart Tank
by
jamie
[
Today
at 01:55:25 am]
Problems with creating a ...
by
jamie
[
Today
at 12:10:44 am]
Fpcupdeluxe
by
cdbc
[July 10, 2026, 11:26:16 pm]
Source Editor Collapsed P...
by
Martin_fr
[July 10, 2026, 11:18:30 pm]
val return code
by
Paolo
[July 10, 2026, 10:58:32 pm]
Gtk3 becomes default widg...
by
PascalDragon
[July 10, 2026, 09:59:25 pm]
Seeking performance feedb...
by
Xenno
[July 10, 2026, 04:31:34 pm]
Pls support Double Comman...
by
Alexx2000
[July 10, 2026, 04:02:56 pm]
Ignoring Exceptions - Hit...
by
Martin_fr
[July 10, 2026, 03:50:47 pm]
Lazarus 4.6 may erase you...
by
Benoni_Edin
[July 10, 2026, 01:22:02 pm]
Greyed out controls?
by
LeP
[July 10, 2026, 12:21:25 pm]
Creating binary
by
marcov
[July 10, 2026, 11:25:17 am]
Hello. I'm new to this co...
by
onionmixer
[July 10, 2026, 03:55:53 am]
Selecting from a list of ...
by
J-G
[July 09, 2026, 11:38:39 pm]
Help needed on how to do ...
by
paweld
[July 09, 2026, 10:06:31 pm]
A recreation of the class...
by
count
[July 09, 2026, 08:53:45 pm]
GTK3 is now the default w...
by
bonmario
[July 09, 2026, 07:48:59 pm]
using bass to play sounds
by
Thaddy
[July 09, 2026, 05:57:43 pm]
DDE
by
Thaddy
[July 09, 2026, 05:01:24 pm]
« previous
next »
Print
Pages: [
1
]
Author
Topic: [SOLVED] Help with TFPGMAP sort... (Read 2467 times)
Espectr0
Full Member
Posts: 240
[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: 791
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: 173
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: 240
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: 2070
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: 240
[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