Page 1 of 1
sortarray, sortstructuredarray
Posted: Wed May 26, 2021 3:59 pm
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.
Re: sortarray, sortstructuredarray
Posted: Wed May 26, 2021 11:24 pm
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
Re: sortarray, sortstructuredarray
Posted: Thu May 27, 2021 10:02 am
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
Re: sortarray, sortstructuredarray
Posted: Thu May 27, 2021 6:53 pm
by mk-soft
It says so in the PB help. For arrays with structures, "SortStructuredArray" MUST be used.
Re: sortarray, sortstructuredarray
Posted: Fri May 28, 2021 8:44 am
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
Re: sortarray, sortstructuredarray
Posted: Fri May 28, 2021 2:19 pm
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!!