sorting files

Discuss any general programming issues here
Post Reply
xabierpayet
Posts: 168
Joined: Fri Feb 24, 2012 9:34 am

sorting files

Post by xabierpayet » Sat Oct 06, 2018 1:08 pm

i have this silly code example

for a=1 to 10
a$={name=rnd(10),num=rnd(10),pos=a}
next

i can sort directly a$.num or a$.pos using the sort command?

User avatar
airsoftsoftwair
Posts: 2668
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

Re: sorting files

Post by airsoftsoftwair » Sat Oct 06, 2018 10:44 pm

Sure, just use a custom callback with Sort() that compares the values and tells Hollywood which entry comes first.

xabierpayet
Posts: 168
Joined: Fri Feb 24, 2012 9:34 am

Re: sorting files

Post by xabierpayet » Wed Oct 10, 2018 11:49 pm

i´m following the example supplied in the guide, and i still with the same problem

nums={}
For a=0 To 10
nums[a]={a=Rnd(10),b=100-Rnd(10)}
NPrint(nums[a].b)
Next
Sort(nums, Function(a,b) Return(nums[a].b > nums.b) EndFunction)
NPrint("")
For k = 0 To 10
NPrint(nums[k].b)
Next
WaitLeftMouse()

why i can not to sort the nums.b array?

User avatar
jPV
Posts: 193
Joined: Sat Mar 26, 2016 10:44 am
Location: RNO
Contact:

Re: sorting files

Post by jPV » Thu Oct 11, 2018 3:56 pm

Change the sort line to:
Sort(nums, Function(a,b) Return(a.b > b.b) EndFunction)

You don't compare the original nums table, but the local a and b which are given to the function.

So with you original situation you'd probaly use something like this:
Sort(nums, Function(a,b) Return(a.num > b.num) EndFunction)

xabierpayet
Posts: 168
Joined: Fri Feb 24, 2012 9:34 am

Re: sorting files

Post by xabierpayet » Thu Oct 11, 2018 8:23 pm

Thanks for the answer, i test your example, and i have this error

Error in line 6 (Unnamed1): Table field 1.71152e-316 was not initialized!

any idea about why this happens?

User avatar
jPV
Posts: 193
Joined: Sat Mar 26, 2016 10:44 am
Location: RNO
Contact:

Re: sorting files

Post by jPV » Thu Oct 11, 2018 9:15 pm

I got that kind of error with your original example, but when I changed like I said, it works for me...

So this is how it works here at least:

nums={}
For a=0 To 10
nums[a]={a=Rnd(10),b=100-Rnd(10)}
NPrint(nums[a].b)
Next
Sort(nums, Function(a,b) Return(a.b > b.b) EndFunction)
NPrint("")
For k = 0 To 10
NPrint(nums[k].b)
Next
WaitLeftMouse()

xabierpayet
Posts: 168
Joined: Fri Feb 24, 2012 9:34 am

Re: sorting files

Post by xabierpayet » Thu Oct 11, 2018 10:27 pm

nice, it´s working now, thanks a lot

Post Reply