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
IfThenStr does not work -...
by
Thaddy
[
Today
at 01:08:25 pm]
NIL vs. Assign: when to u...
by
Warfley
[
Today
at 12:44:07 pm]
GTK3 still alpha
by
AlexTP
[
Today
at 12:43:36 pm]
[LazFreeType] effect on t...
by
domasz
[
Today
at 12:40:51 pm]
Segmentation fault
by
cdbc
[
Today
at 12:33:57 pm]
efficiency problem
by
jamie
[
Today
at 12:33:38 pm]
The Silver Coder on YouTu...
by
silvercoder70
[
Today
at 11:05:36 am]
LazTTF2Vector - An Enhanc...
by
domasz
[
Today
at 08:03:49 am]
App crashes in main menu ...
by
Igor Kokarev
[
Today
at 08:02:20 am]
to read an UTF8 text file
by
cdbc
[
Today
at 07:41:32 am]
Force parameters of my ap...
by
cdbc
[
Today
at 07:33:04 am]
Converting a Project from...
by
egsuh
[
Today
at 07:21:45 am]
Connection to MySQL versi...
by
paweld
[
Today
at 06:32:04 am]
Error after installing ne...
by
sbulazel
[
Today
at 02:23:32 am]
PostgreSQL "no route to h...
by
wcage03
[March 15, 2025, 11:25:04 pm]
Can't start Lazarus 4.0 R...
by
mykyl
[March 15, 2025, 10:54:48 pm]
TControlBar and hence the...
by
paule32
[March 15, 2025, 10:20:25 pm]
PScript or MagicScript ad...
by
eldonfsr
[March 15, 2025, 09:44:53 pm]
Hooking to `OnISupport` d...
by
Remy Lebeau
[March 15, 2025, 09:42:20 pm]
Hustle - A simple task ma...
by
Hansvb
[March 15, 2025, 07:42:04 pm]
Bindings for PlutoVG
by
CynicRus
[March 15, 2025, 07:09:50 pm]
zeosdb zupdate don't work...
by
eldonfsr
[March 15, 2025, 06:11:23 pm]
How to Build Laz IDE from...
by
marcov
[March 15, 2025, 04:51:09 pm]
[Solved] Exception "In Ha...
by
cdbc
[March 15, 2025, 04:34:14 pm]
File IO issues
by
dryzone
[March 15, 2025, 03:58:23 pm]
« previous
next »
Print
Pages: [
1
]
Author
Topic: Identify sorted data (ascending sorting is not recognized) (Read 996 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: 196
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