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
WIKI Timeout issues
Please read here if you have trouble connecting to the wiki
Recent
Lazarus 4 - I give up and...
by
Martin_fr
[
Today
at 06:02:23 pm]
CEF component - the first...
by
Nicole
[
Today
at 05:32:02 pm]
How to run an external pr...
by
Leledumbo
[
Today
at 05:00:21 pm]
Free Pascal Database Serv...
by
Leledumbo
[
Today
at 04:52:47 pm]
Font character being clip...
by
dsiders
[
Today
at 04:43:31 pm]
Feature request/suggestio...
by
440bx
[
Today
at 04:28:12 pm]
FPC for high-performance ...
by
Thaddy
[
Today
at 03:14:33 pm]
functional IF
by
munair
[
Today
at 01:54:44 pm]
Build failure
by
user5
[
Today
at 11:36:56 am]
Things go bad when not us...
by
Thaddy
[
Today
at 11:33:24 am]
TimageList - Lazarus 4 wi...
by
Nicole
[
Today
at 11:15:08 am]
Firebird 5.02. - the root...
by
Nicole
[
Today
at 11:09:43 am]
Data\Watch Breakpoint by ...
by
Martin_fr
[
Today
at 10:56:03 am]
Reading Embeded PO Files
by
Thaddy
[
Today
at 10:54:41 am]
OpenDialog and mouse even...
by
hedgehog
[
Today
at 09:43:17 am]
A Challenge for Lazarus G...
by
MarkMLl
[
Today
at 09:13:22 am]
dBGRidController and Erro...
by
essence-ciel
[
Today
at 06:31:37 am]
[AGREED] processing web f...
by
ADMGNS
[
Today
at 05:28:53 am]
What is the current versi...
by
Remy Lebeau
[
Today
at 04:18:49 am]
Silver Pascal Coder - Gre...
by
TBMan
[
Today
at 03:09:22 am]
FPReport designer, am I u...
by
dseligo
[
Today
at 01:19:22 am]
X11Libre, finally and for...
by
Fred vS
[June 15, 2025, 09:44:08 pm]
PZ_Nes emulator
by
Seenkao
[June 15, 2025, 09:15:28 pm]
ZenGL +android + MacOS Co...
by
Seenkao
[June 15, 2025, 08:57:55 pm]
64 bits and PascalScript
by
maurog
[June 15, 2025, 08:27:56 pm]
« previous
next »
Print
Pages: [
1
]
Author
Topic: Identify sorted data (ascending sorting is not recognized) (Read 1036 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: 202
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