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
Help with search and repa...
by
CM630
[
Today
at 12:54:09 pm]
Heaptrc reports unfreed m...
by
Hartmut
[
Today
at 10:33:47 am]
Initialize list in functi...
by
PeterHu
[
Today
at 10:33:27 am]
Lazarus Blowfish
by
Thaddy
[
Today
at 09:30:07 am]
Tips for cross-compiling ...
by
Seenkao
[
Today
at 09:25:29 am]
An Idea for a helpful uti...
by
Thaddy
[
Today
at 08:27:38 am]
Drawing a picture on the ...
by
DMH
[
Today
at 05:01:07 am]
Anyone interested in help...
by
ad1mt
[November 09, 2025, 07:57:13 pm]
How detect a keypress in ...
by
Remy Lebeau
[November 09, 2025, 07:49:50 pm]
Paradox date filter
by
tawfiq-lz
[November 09, 2025, 07:46:20 pm]
Why is there no Chinese s...
by
Ten_Mile_Hike
[November 09, 2025, 06:55:11 pm]
Convert table to JSON arr...
by
paweld
[November 09, 2025, 05:56:56 pm]
Unexpected lack of error ...
by
n7800
[November 09, 2025, 05:39:10 pm]
FPC 3.2.4-rc1 available
by
pleumann
[November 09, 2025, 05:12:32 pm]
application crashes on st...
by
Thaddy
[November 09, 2025, 04:17:28 pm]
Blog area?
by
Martin_fr
[November 09, 2025, 02:00:17 pm]
SimpleXML
by
ednaldomoreira
[November 09, 2025, 01:45:18 pm]
What happens if you use "...
by
Thaddy
[November 09, 2025, 01:39:12 pm]
CustomDraw package compil...
by
lainz
[November 09, 2025, 01:21:58 pm]
Register global hotkey
by
Thaddy
[November 09, 2025, 12:46:46 pm]
How to port TArray.Sort?
by
Thaddy
[November 09, 2025, 12:20:36 pm]
Dynamic libraries in Linu...
by
Fred vS
[November 08, 2025, 10:28:42 pm]
FPC v3.2.2 internal error
by
440bx
[November 08, 2025, 09:15:48 pm]
Distance of gridlines in ...
by
wp
[November 08, 2025, 08:08:14 pm]
Lazarus portable?
by
PascalDragon
[November 08, 2025, 06:29:28 pm]
« previous
next »
Print
Pages: [
1
]
Author
Topic: [SOLVED] Help with TFPGMAP sort... (Read 1615 times)
Espectr0
Full Member
Posts: 233
[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: 755
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: 169
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: 233
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: 2021
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: 233
[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