sorting files

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

sorting files

Post 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?

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

Re: sorting files

Post by airsoftsoftwair »

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

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

Re: sorting files

Post 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?

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

Re: sorting files

Post 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)

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

Re: sorting files

Post 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?

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

Re: sorting files

Post 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()

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

Re: sorting files

Post by xabierpayet »

nice, it´s working now, thanks a lot

Post Reply