Recent

Author Topic: Identify sorted data (ascending sorting is not recognized)  (Read 916 times)

ChrisP

  • New Member
  • *
  • Posts: 14
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][+][-]
  1. // Implementation of AnalyzeDataSorting
  2. function TFormDataimport.AnalyzeDataSorting(const Data: TStringList): Integer;
  3. var
  4.   I: Integer;
  5.   Ascending, Descending: Boolean;
  6. begin
  7.   if Data.Count = 0 then
  8.     Exit(0); // Empty list
  9.  
  10.   Ascending := True;
  11.   Descending := True;
  12.  
  13.   for I := 0 to Data.Count - 2 do
  14.   begin
  15.     if CompareText(Data[I], Data[I+1]) > 0 then
  16.       Ascending := False;
  17.     if CompareText(Data[I], Data[I+1]) < 0 then
  18.       Descending := False;
  19.  
  20.     // If neither ascending nor descending, exit the loop
  21.     if not Ascending and not Descending then
  22.       Break;
  23.   end;
  24.  
  25.   if Ascending then
  26.     Exit(1); // Sorted ascending
  27.  
  28.   if Descending then
  29.     Exit(2); // Sorted descending
  30.  
  31.   // If neither ascending nor descending, check for partial sorting
  32.   Ascending := False;
  33.   Descending := False;
  34.   for I := 0 to Data.Count - 2 do
  35.   begin
  36.     if CompareText(Data[I], Data[I+1]) < 0 then
  37.       Ascending := True;
  38.     if CompareText(Data[I], Data[I+1]) > 0 then
  39.       Descending := True;
  40.  
  41.     if Ascending and Descending then
  42.     begin
  43.       Exit(3); // Partially sorted
  44.     end;
  45.   end;
  46.  
  47.   Result := 4; // Randomly sorted
  48. end;
  49.  

Many thanks for any help!

Best regards
Chris
« Last Edit: May 27, 2024, 10:50:21 am by ChrisP »

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?
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!

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2269
  • Fifty shades of code.
    • Delphi & FreePascal
Re: Identify sorted data (ascending sorting is not recognized)
« Reply #3 on: May 27, 2024, 12:03:17 pm »
Try:
Code: Pascal  [Select][+][-]
  1. {Possible results:
  2. -1 = empty list
  3.  0 = unsorted
  4.  1 = ascending
  5.  2 = descending
  6.  3 = partial sorted
  7.  4 = input got no differences
  8.  5 = just 1 item}
  9. function CheckSortOrder(const AInput: TStrings): Integer;
  10. var
  11.   i: Integer;
  12.   Ascending, Descending, Partial, IsSame: Boolean;
  13.   s1, s2: string;
  14. begin
  15.   if AInput.Count = 0 then
  16.     Exit(-1);
  17.   if AInput.Count = 1 then
  18.     Exit(5);
  19.   Ascending := True;
  20.   Descending := True;
  21.   IsSame := True;
  22.   Partial := False;
  23.   for i := 0 to Pred(AInput.Count) do
  24.     if i < Pred(AInput.Count) then
  25.       begin
  26.         s1 := Trim(AInput[i]);
  27.         s2 := Trim(AInput[Succ(i)]);
  28.         if (not SameText(s1, s2)) then
  29.           begin
  30.             IsSame := False;
  31.             if CompareText(s1, s2) > 0 then
  32.               Ascending := False
  33.             else
  34.               Descending := False;
  35.             if (not Ascending) and (not Descending) and (i > 0) then
  36.               Partial := True;
  37.             if (not Ascending) and (not Descending) then
  38.               Break;
  39.           end;
  40.       end;
  41.   if (not Ascending) and (not Descending) and (not Partial) then
  42.     Result := 0;
  43.   if Ascending and (not Descending) and (not Partial) then
  44.     Result := 1;
  45.   if (not Ascending) and Descending and (not Partial) then
  46.     Result := 2;
  47.   if Partial then
  48.     Result := 3;
  49.   if IsSame then
  50.     Result := 4;
  51. 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 »
« 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!

 

TinyPortal © 2005-2018