Lazarus
Home
Help
TinyPortal
Search
Login
Register
Lazarus
»
Forum
»
Programming
»
General
»
Identify sorted data (ascending sorting is not recognized)
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
how to use SetSysColors?
by
Zvoni
[
Today
at 03:41:35 pm]
Forum slow?
by
TRon
[
Today
at 03:32:07 pm]
Add Artificial Intelligen...
by
VisualLab
[
Today
at 03:23:30 pm]
Deal or no Deal graphic
by
TBMan
[
Today
at 02:57:28 pm]
Pac man and Missile Comma...
by
TBMan
[
Today
at 02:51:05 pm]
Bug and crash. I think th...
by
VisualLab
[
Today
at 02:51:00 pm]
A basic windows program s...
by
TRon
[
Today
at 02:36:43 pm]
Tesseract for ocr
by
rvk
[
Today
at 02:30:29 pm]
Random Carpet Designs: La...
by
Boleeman
[
Today
at 02:21:29 pm]
New Package to use WIA sc...
by
MaxM74
[
Today
at 01:25:49 pm]
Looking for better way...
by
alpine
[
Today
at 01:21:23 pm]
read text file position b...
by
Packs
[
Today
at 01:09:39 pm]
Context Sensitive Help fo...
by
marcov
[
Today
at 12:36:26 pm]
Troubles with function de...
by
alpine
[
Today
at 12:06:45 pm]
[Solved]Generic methods i...
by
cdbc
[
Today
at 11:47:01 am]
SplitString issue
by
Thaddy
[
Today
at 11:20:17 am]
changing the position of ...
by
kjteng
[
Today
at 10:48:10 am]
Usage of Lazlogger 2 - st...
by
PeterX
[
Today
at 10:08:04 am]
Generic function is 'wrea...
by
cdbc
[
Today
at 09:54:06 am]
Image processing
by
MarkMLl
[
Today
at 09:10:54 am]
show popupmenu in listvie...
by
ASerge
[
Today
at 06:19:40 am]
Accessing parent form fro...
by
nikel
[
Today
at 03:26:35 am]
fpspreadsheet (Spready) -...
by
tatamata
[
Today
at 01:34:00 am]
Crosshair following Point...
by
chucky
[February 11, 2025, 11:41:11 pm]
Custom self drawn compone...
by
VisualLab
[February 11, 2025, 11:18:35 pm]
« previous
next »
Print
Pages: [
1
]
Author
Topic: Identify sorted data (ascending sorting is not recognized) (Read 953 times)
ChrisP
New Member
Posts: 14
Identify sorted data (ascending sorting is not recognized)
«
on:
May 27, 2024, 10:23:47 am »
Hello everyone,
In the following function, I have the problem that an ascending sort (e.g. 1, 2, 3, 4, 5, 6 or A, B, C, D, E, F, G) is incorrectly recognized as a partial sort. A descending sort is recognized without error. The same applies if a sort is partially sorted. What could be the reason for this?
Code: Pascal
[Select]
[+]
[-]
// Implementation of AnalyzeDataSorting
function
TFormDataimport
.
AnalyzeDataSorting
(
const
Data
:
TStringList
)
:
Integer
;
var
I
:
Integer
;
Ascending
,
Descending
:
Boolean
;
begin
if
Data
.
Count
=
0
then
Exit
(
0
)
;
// Empty list
Ascending
:
=
True
;
Descending
:
=
True
;
for
I
:
=
0
to
Data
.
Count
-
2
do
begin
if
CompareText
(
Data
[
I
]
,
Data
[
I
+
1
]
)
>
0
then
Ascending
:
=
False
;
if
CompareText
(
Data
[
I
]
,
Data
[
I
+
1
]
)
<
0
then
Descending
:
=
False
;
// If neither ascending nor descending, exit the loop
if
not
Ascending
and
not
Descending
then
Break
;
end
;
if
Ascending
then
Exit
(
1
)
;
// Sorted ascending
if
Descending
then
Exit
(
2
)
;
// Sorted descending
// If neither ascending nor descending, check for partial sorting
Ascending
:
=
False
;
Descending
:
=
False
;
for
I
:
=
0
to
Data
.
Count
-
2
do
begin
if
CompareText
(
Data
[
I
]
,
Data
[
I
+
1
]
)
<
0
then
Ascending
:
=
True
;
if
CompareText
(
Data
[
I
]
,
Data
[
I
+
1
]
)
>
0
then
Descending
:
=
True
;
if
Ascending
and
Descending
then
begin
Exit
(
3
)
;
// Partially sorted
end
;
end
;
Result
:
=
4
;
// Randomly sorted
end
;
Many thanks for any help!
Best regards
Chris
«
Last Edit: May 27, 2024, 10:50:21 am by ChrisP
»
Logged
RayoGlauco
Full Member
Posts: 194
Beers: 1567
Re: Identify sorted data (ascending sorting is not recognized)
«
Reply #1 on:
May 27, 2024, 11:02:45 am »
I tested your code with values ['1','2','3','4','5','6','7'] and ['A','B','C','D','E','F','G'].
Your function returns 1 as expected.
Maybe there is some issue about your data?
Logged
To err is human, but to really mess things up, you need a computer.
ChrisP
New Member
Posts: 14
Re: Identify sorted data (ascending sorting is not recognized)
«
Reply #2 on:
May 27, 2024, 11:20:50 am »
@RayoGlauco
You are right. It was the data.
As soon as there are spaces in between, it no longer works:
values ['1',' 2',' 3',' 4',' 5',' 6',' 7'] and [' A',' B',' C',' D',' E',' F',' G'].
I will adapt the code! :-)
Thank you!
Logged
KodeZwerg
Hero Member
Posts: 2269
Fifty shades of code.
Re: Identify sorted data (ascending sorting is not recognized)
«
Reply #3 on:
May 27, 2024, 12:03:17 pm »
Try:
Code: Pascal
[Select]
[+]
[-]
{Possible results:
-1 = empty list
0 = unsorted
1 = ascending
2 = descending
3 = partial sorted
4 = input got no differences
5 = just 1 item}
function
CheckSortOrder
(
const
AInput
:
TStrings
)
:
Integer
;
var
i
:
Integer
;
Ascending
,
Descending
,
Partial
,
IsSame
:
Boolean
;
s1
,
s2
:
string
;
begin
if
AInput
.
Count
=
0
then
Exit
(
-
1
)
;
if
AInput
.
Count
=
1
then
Exit
(
5
)
;
Ascending
:
=
True
;
Descending
:
=
True
;
IsSame
:
=
True
;
Partial
:
=
False
;
for
i
:
=
0
to
Pred
(
AInput
.
Count
)
do
if
i <
Pred
(
AInput
.
Count
)
then
begin
s1
:
=
Trim
(
AInput
[
i
]
)
;
s2
:
=
Trim
(
AInput
[
Succ
(
i
)
]
)
;
if
(
not
SameText
(
s1
,
s2
)
)
then
begin
IsSame
:
=
False
;
if
CompareText
(
s1
,
s2
)
>
0
then
Ascending
:
=
False
else
Descending
:
=
False
;
if
(
not
Ascending
)
and
(
not
Descending
)
and
(
i >
0
)
then
Partial
:
=
True
;
if
(
not
Ascending
)
and
(
not
Descending
)
then
Break
;
end
;
end
;
if
(
not
Ascending
)
and
(
not
Descending
)
and
(
not
Partial
)
then
Result
:
=
0
;
if
Ascending
and
(
not
Descending
)
and
(
not
Partial
)
then
Result
:
=
1
;
if
(
not
Ascending
)
and
Descending
and
(
not
Partial
)
then
Result
:
=
2
;
if
Partial
then
Result
:
=
3
;
if
IsSame
then
Result
:
=
4
;
end
;
It remove surrounding spaces at check
Ps: I would remove "Partial" since everything that is neither asc nor desc will become partial.
«
Last Edit: May 27, 2024, 12:42:48 pm by KodeZwerg
»
Logged
«
Last Edit:
Tomorrow
at 31:76:97 xm by
KodeZwerg
»
ChrisP
New Member
Posts: 14
Re: Identify sorted data (ascending sorting is not recognized)
«
Reply #4 on:
May 28, 2024, 08:59:50 am »
@KodeZwerg
Thank you very much!
It works perfectly now!
Logged
Print
Pages: [
1
]
« previous
next »
Lazarus
»
Forum
»
Programming
»
General
»
Identify sorted data (ascending sorting is not recognized)
TinyPortal
© 2005-2018