Page 1 of 1

sorting files

Posted: Sat Oct 06, 2018 1:08 pm
by xabierpayet
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?

Re: sorting files

Posted: Sat Oct 06, 2018 10:44 pm
by airsoftsoftwair
Sure, just use a custom callback with Sort() that compares the values and tells Hollywood which entry comes first.

Re: sorting files

Posted: Wed Oct 10, 2018 11:49 pm
by xabierpayet
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?

Re: sorting files

Posted: Thu Oct 11, 2018 3:56 pm
by jPV
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)

Re: sorting files

Posted: Thu Oct 11, 2018 8:23 pm
by xabierpayet
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?

Re: sorting files

Posted: Thu Oct 11, 2018 9:15 pm
by jPV
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()

Re: sorting files

Posted: Thu Oct 11, 2018 10:27 pm
by xabierpayet
nice, it´s working now, thanks a lot