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
Boolean type
by
Martin_fr
[April 22, 2025, 11:53:24 pm]
Detecting symbol correspo...
by
n7800
[April 22, 2025, 11:35:04 pm]
$5 task: put the IDE mod ...
by
Fibonacci
[April 22, 2025, 11:35:03 pm]
I didn't expect this
by
440bx
[April 22, 2025, 11:25:14 pm]
Source code explorer - te...
by
d2010
[April 22, 2025, 11:21:41 pm]
Named range
by
wp
[April 22, 2025, 11:01:53 pm]
[SOLVED] WebP images and ...
by
TRon
[April 22, 2025, 10:54:50 pm]
Testing with SQLite3DataS...
by
1HuntnMan
[April 22, 2025, 10:50:23 pm]
Cannot execute Lazarus ID...
by
maurobio
[April 22, 2025, 10:46:07 pm]
Writing a TMemo widget fr...
by
paule32
[April 22, 2025, 10:26:27 pm]
calculate position of TBu...
by
paule32
[April 22, 2025, 09:33:16 pm]
Recamán's Sequence Curve ...
by
TRon
[April 22, 2025, 08:36:54 pm]
FPC 3.2.2 using Result wi...
by
n7800
[April 22, 2025, 08:26:20 pm]
MVP made easier.
by
cdbc
[April 22, 2025, 08:07:40 pm]
Math.RoundTo incorrect so...
by
BeniBela
[April 22, 2025, 07:42:15 pm]
Amigo programming languag...
by
Packs
[April 22, 2025, 07:15:40 pm]
Rain Simulator
by
d2010
[April 22, 2025, 07:02:49 pm]
Array not reading element...
by
Khrys
[April 22, 2025, 04:22:28 pm]
Korean Tiles (and other p...
by
Boleeman
[April 22, 2025, 03:56:30 pm]
Does anyone have the idea...
by
TYDQ
[April 22, 2025, 03:56:11 pm]
macro for code ?
by
Thaddy
[April 22, 2025, 02:18:47 pm]
How to compile my.lpr for...
by
d2010
[April 22, 2025, 01:09:54 pm]
Online Package Manager
by
d.ioannidis
[April 22, 2025, 11:34:26 am]
Using a custom record as ...
by
Martin_fr
[April 22, 2025, 10:45:23 am]
FPC Deluxe doesn't work
by
coradi
[April 22, 2025, 10:04:36 am]
« previous
next »
Print
Pages: [
1
]
Author
Topic: Identify sorted data (ascending sorting is not recognized) (Read 1006 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