sortarray, sortstructuredarray

Everything else that doesn't fall into one of the other PB categories.
eck49
Enthusiast
Enthusiast
Posts: 153
Joined: Sat Nov 14, 2020 10:08 pm
Location: England

sortarray, sortstructuredarray

Post by eck49 »

I used SortArray when I should have used SortStructuredArray. The results were the same.
Is this simply because the sort field was the first field in each element?
Each element consisted of 3 integer fields.
Ubuntu 22.04 64-bit
Purebasic 6.00 (as of 5 Sep 2022)
(native tongue: English)
User avatar
FourthStone
User
User
Posts: 26
Joined: Mon Dec 11, 2017 8:44 am
Location: Australia

Re: sortarray, sortstructuredarray

Post by FourthStone »

I tried this test, I got an error saying bad parameter type, array expected.

EDIT: Actually it worked when I fixed my typo... but I can't get the SortStructuredArray to work...

EDIT2: Got it working, the first sortarray didn't sort anything... the structured sort is arranged by the specified x parameter.

Code: Select all

Structure Test
  x.i
  y.i
  z.i
EndStructure

Dim aTest.Test(10)

For i=0 To 10
  aTest(i)\x=Random(100)
  aTest(i)\y=Random(100)
  aTest(i)\z=Random(100)
Next

SortArray(aTest(),#PB_Sort_Ascending)

For i=0 To 10
  Debug Str(i)+"  x:"+Str(aTest(i)\x)+"  x:"+Str(aTest(i)\y)+"  x:"+Str(aTest(i)\z)  
Next

SortStructuredArray(aTest(),#PB_Sort_Descending,OffsetOf(Test\x), TypeOf(Test\x) )

For i=0 To 10
  Debug Str(i)+"  x:"+Str(aTest(i)\x)+"  x:"+Str(aTest(i)\y)+"  x:"+Str(aTest(i)\z)  
Next


End

; debug out:
;0   x:6   x:99  x:40
;1   x:55  x:4   x:92
;2   x:61  x:59  x:89
;3   x:8   x:73  x:92
;4   x:42  x:68  x:25
;5   x:48  x:13  x:0
;6   x:43  x:39  x:74
;7   x:34  x:72  x:21
;8   x:87  x:86  x:93
;9   x:94  x:48  x:56
;10  x:50  x:4   x:0
;0   x:94  x:48  x:56
;1   x:87  x:86  x:93
;2   x:61  x:59  x:89
;3   x:55  x:4   x:92
;4   x:50  x:4   x:0
;5   x:48  x:13  x:0
;6   x:43  x:39  x:74
;7   x:42  x:68  x:25
;8   x:34  x:72  x:21
;9   x:8   x:73  x:92
;10  x:6   x:99  x:40
eck49
Enthusiast
Enthusiast
Posts: 153
Joined: Sat Nov 14, 2020 10:08 pm
Location: England

Re: sortarray, sortstructuredarray

Post by eck49 »

That's strange. In my real code, the arrays were much more than 10, but the first 20 or so elements and the last one after sorting were the same each way. I only noticed the matter because I was chasing down a bug which turned out to be nothing to do with this.

I don't propose to follow this up! My unstructured sort was a bug in any case.

To make the test more comparable, I adapted your code to make both sorts descending and to sort the same data pre-sort. Just as different as your result.

Code: Select all

Structure Test
  x.i
  y.i
  z.i
EndStructure

Dim aTest.Test(10)
Dim bTest.Test(10)

For i=0 To 10
  aTest(i)\x=Random(100)
  aTest(i)\y=Random(100)
  aTest(i)\z=Random(100)
Next

CopyArray(aTest(),bTest())

SortArray(bTest(),#PB_Sort_Descending)

For i=0 To 10
  Debug Str(i)+"  x:"+Str(bTest(i)\x)+"  x:"+Str(bTest(i)\y)+"  x:"+Str(bTest(i)\z)  
Next

SortStructuredArray(aTest(),#PB_Sort_Descending,OffsetOf(Test\x), TypeOf(Test\x) )

For i=0 To 10
  Debug Str(i)+"  x:"+Str(aTest(i)\x)+"  x:"+Str(aTest(i)\y)+"  x:"+Str(aTest(i)\z)  
Next


End

Ubuntu 22.04 64-bit
Purebasic 6.00 (as of 5 Sep 2022)
(native tongue: English)
User avatar
mk-soft
Always Here
Always Here
Posts: 5333
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: sortarray, sortstructuredarray

Post by mk-soft »

It says so in the PB help. For arrays with structures, "SortStructuredArray" MUST be used.
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
kernadec
Enthusiast
Enthusiast
Posts: 146
Joined: Tue Jan 05, 2010 10:35 am

Re: sortarray, sortstructuredarray

Post by kernadec »

hi, eck49
cordially

Code: Select all

Structure Test
  x.i
  y.i
  z.i
EndStructure

Dim aTest.Test(10)
Dim bTest.Test(10)

For i=0 To 10
  aTest(i)\x=Random(100)
  aTest(i)\y=Random(100)
  aTest(i)\z=Random(100)
Next

CopyArray(aTest(),bTest())

SortStructuredArray(bTest(), #PB_Sort_Ascending, OffsetOf(Test\x), TypeOf(Test\x))

For i=0 To 10
  Debug Str(i)+"  x:"+Str(bTest(i)\x)+"  x:"+Str(bTest(i)\y)+"  x:"+Str(bTest(i)\z)  
Next

SortStructuredArray(aTest(), #PB_Sort_Descending, OffsetOf(Test\x), TypeOf(Test\x) )

For i=0 To 10
  Debug Str(10-i)+"  x:"+Str(aTest(i)\x)+"  x:"+Str(aTest(i)\y)+"  x:"+Str(aTest(i)\z)  
Next


End
eck49
Enthusiast
Enthusiast
Posts: 153
Joined: Sat Nov 14, 2020 10:08 pm
Location: England

Re: sortarray, sortstructuredarray

Post by eck49 »

@kernadec

We are on the same page with this.

Perhaps I should have pointed out in my previous post that the code I included there was showing the difference between SortArray (WRONG) and SortStructuredArray (RIGHT) applied to structured arrays with identical contents. It's a pity the compiler does not complain that the type of sort does not fit the type of array, but I can't expect other people's code - in this case the PB compiler - to be perfectly aligned to my wishes when even mine isn't!!
Ubuntu 22.04 64-bit
Purebasic 6.00 (as of 5 Sep 2022)
(native tongue: English)
Post Reply