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
TSQLquery does not save a...
by
egsuh
[
Today
at 08:52:36 am]
[SOLEVD] Unwanted additio...
by
CM630
[
Today
at 08:32:42 am]
ЛазМер/ LazMer for measur...
by
CM630
[
Today
at 08:22:23 am]
Compile auxilliary units ...
by
Wesbat
[
Today
at 08:17:22 am]
How to combine data from ...
by
Zvoni
[
Today
at 08:13:09 am]
Nested declarations insid...
by
440bx
[
Today
at 06:02:53 am]
Lazarus Pascal Keywords a...
by
dbannon
[
Today
at 05:34:52 am]
New old guy
by
TBMan
[
Today
at 04:00:47 am]
How to validate ParamStr(...
by
n7800
[
Today
at 03:29:56 am]
Why with allows assignmen...
by
440bx
[
Today
at 03:02:26 am]
SDL2: Runtime Error 216 o...
by
TRon
[
Today
at 02:52:21 am]
Lazarus Release Candidate...
by
calebs
[
Today
at 01:55:45 am]
MQTT Client
by
cdbc
[January 12, 2025, 10:48:04 pm]
SDL2 image rotation Probl...
by
Fred vS
[January 12, 2025, 09:30:11 pm]
FPC crashes after running...
by
jamie
[January 12, 2025, 09:20:00 pm]
Out of memory / filter tw...
by
BubikolRamios
[January 12, 2025, 08:32:02 pm]
rebuilding IDE fails
by
Martin_fr
[January 12, 2025, 08:24:54 pm]
How do I access data memo...
by
TBMan
[January 12, 2025, 08:10:18 pm]
Out of heap
by
BubikolRamios
[January 12, 2025, 07:53:17 pm]
Where do I get IdHTTP, ...
by
Thaddy
[January 12, 2025, 07:03:36 pm]
TSQLQuery with lookup fie...
by
dpap
[January 12, 2025, 06:56:57 pm]
Vulnerabilities lurking i...
by
tetrastes
[January 12, 2025, 06:25:19 pm]
[Solved]Help needed with ...
by
cdbc
[January 12, 2025, 06:01:02 pm]
Large or huge sets
by
Thaddy
[January 12, 2025, 05:22:06 pm]
Pascal and Musical Tones ...
by
Roland57
[January 12, 2025, 04:11:37 am]
« previous
next »
Print
Pages: [
1
]
Author
Topic: Identify sorted data (ascending sorting is not recognized) (Read 916 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