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
About donations (wiki)
Bookstore
Computer Math and Games in Pascal
(preview)
Lazarus Handbook
Search
Advanced search
Recent
file Blockread error
by
TBMan
[
Today
at 03:45:29 am]
[SOLVED]Might have a menu...
by
Tony Stone
[
Today
at 03:30:15 am]
Lazarus Release 3.6
by
dbannon
[
Today
at 03:25:19 am]
Nested declarations insid...
by
440bx
[
Today
at 02:24:16 am]
how to create sync applic...
by
ASerge
[
Today
at 01:40:33 am]
test if web site is valid
by
Warfley
[
Today
at 01:22:48 am]
File operations blocking ...
by
TRon
[
Today
at 01:15:30 am]
[Solved] TPopupMenu OnDra...
by
d7_2_laz
[
Today
at 12:37:26 am]
how to make application r...
by
DragoRosso
[
Today
at 12:19:23 am]
Discussion: How do you di...
by
dbannon
[
Today
at 12:18:30 am]
Assigning proc to event a...
by
Bart
[January 13, 2025, 10:16:59 pm]
SDL2 image rotation Probl...
by
TRon
[January 13, 2025, 10:00:09 pm]
Why with allows assignmen...
by
PascalDragon
[January 13, 2025, 09:53:19 pm]
Color Buttons
by
lainz
[January 13, 2025, 09:14:49 pm]
Lazarus Pascal Keywords a...
by
PascalDragon
[January 13, 2025, 09:11:42 pm]
Generics as PUBLISHED imp...
by
PascalDragon
[January 13, 2025, 09:04:43 pm]
FP crashes after running ...
by
Martin_fr
[January 13, 2025, 09:03:42 pm]
Random ?
by
Thaddy
[January 13, 2025, 07:26:20 pm]
UnoLib - library in Pasca...
by
ackarwow
[January 13, 2025, 07:08:06 pm]
¿Hay alguna limitación en...
by
e_nigma
[January 13, 2025, 06:27:03 pm]
Exploring FPC/Lazarus uni...
by
MarkMLl
[January 13, 2025, 05:48:05 pm]
Web Pascal without JavaSc...
by
zendrael
[January 13, 2025, 05:19:57 pm]
MainMenu: How to use the ...
by
madref
[January 13, 2025, 03:03:37 pm]
How to combine data from ...
by
Thaddy
[January 13, 2025, 02:30:11 pm]
SDL2: Runtime Error 216 o...
by
mischi
[January 13, 2025, 02:11:35 pm]
« previous
next »
Print
Pages: [
1
]
Author
Topic: [SOLVED] Help with TFPGMAP sort... (Read 1377 times)
Espectr0
Full Member
Posts: 221
[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: 690
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: 153
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: 221
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: 1832
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: 221
[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