Page 1 of 1

SortStructuredArray Ascending

Posted: Sun Aug 08, 2021 10:58 am
by benmalartre
Hello, it appears the SortStructuredArray function, used with #PB_SORT_ASCENDING mode,
is returning a wrong array. a first empty element is added to the array and the one that should be last has disappeared.

The sort is working fine with #PB_SORT_DESCENDING but i have to reverse the array in a second pass.
Is this a normal behavior? looks like a bug to me...


Code: Select all

Structure Point_t
  x.f
  y.f
  z.f
EndStructure

Procedure EchoPoint(*p.Point_t)
  Debug StrF(*p\x, 3)+","+  StrF(*p\y, 3)+","+  StrF(*p\z, 3)
EndProcedure

Dim points.Point_t(12)

Debug "-------------------- RAW ---------------------------"
For i = 0 To 11
  Define *p.Point_t = points(i)
  *p\y = Random(100)
  EchoPoint(*p)
Next

SortStructuredArray(points(), #PB_Sort_Ascending, OffsetOf(Point_t\y), #PB_Float)

Debug "---------------------- SORTED ----------------------"
For i = 0 To 11
  Define *p.Point_t = points(i)
  EchoPoint(*p)
Next
Windows 10, PB 5.73

Re: SortStructuredArray Ascending

Posted: Sun Aug 08, 2021 11:55 am
by User_Russian
Not bug

Code: Select all

Structure Point_t
  x.f
  y.f
  z.f
EndStructure

Procedure EchoPoint(*p.Point_t)
  Debug StrF(*p\x, 3)+","+  StrF(*p\y, 3)+","+  StrF(*p\z, 3)
EndProcedure

Dim points.Point_t(11)

Debug "-------------------- RAW ---------------------------"
For i = 0 To 11
  Define *p.Point_t = points(i)
  *p\y = Random(100)
  EchoPoint(*p)
Next

SortStructuredArray(points(), #PB_Sort_Ascending, OffsetOf(Point_t\y), #PB_Float)

Debug "---------------------- SORTED ----------------------"
For i = 0 To 11
  Define *p.Point_t = points(i)
  EchoPoint(*p)
Next 

Re: SortStructuredArray Ascending

Posted: Sun Aug 08, 2021 11:59 am
by mk-soft
No bug,

An array dim x(12) had a range from 0 to 12 and not from 0 to 11.
But it is often overlooked that an array is always defined with the last element with a base of zero.

Code: Select all

Structure Point_t
  x.f
  y.f
  z.f
EndStructure

Procedure EchoPoint(*p.Point_t)
  Debug StrF(*p\x, 3)+","+  StrF(*p\y, 3)+","+  StrF(*p\z, 3)
EndProcedure

Dim points.Point_t(11)

Debug "-------------------- RAW ---------------------------"
For i = 0 To 11
  Define *p.Point_t = points(i)
  *p\y = Random(100)
  EchoPoint(*p)
Next

SortStructuredArray(points(), #PB_Sort_Ascending, OffsetOf(Point_t\y), #PB_Float)

Debug "---------------------- SORTED ----------------------"
For i = 0 To 11
  Define *p.Point_t = points(i)
  EchoPoint(*p)
Next

Re: SortStructuredArray Ascending

Posted: Sun Aug 15, 2021 8:16 am
by benmalartre
my bad! thanks for the answer!