SortStructuredArray Ascending

Just starting out? Need help? Post your questions and find answers here.
benmalartre
User
User
Posts: 27
Joined: Thu Mar 14, 2013 11:24 am
Location: paris
Contact:

SortStructuredArray Ascending

Post 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
User_Russian
Addict
Addict
Posts: 1443
Joined: Wed Nov 12, 2008 5:01 pm
Location: Russia

Re: SortStructuredArray Ascending

Post 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 
User avatar
mk-soft
Always Here
Always Here
Posts: 5335
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: SortStructuredArray Ascending

Post 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
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
benmalartre
User
User
Posts: 27
Joined: Thu Mar 14, 2013 11:24 am
Location: paris
Contact:

Re: SortStructuredArray Ascending

Post by benmalartre »

my bad! thanks for the answer!
Post Reply