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?
sorting files
- airsoftsoftwair
- Posts: 5443
- Joined: Fri Feb 12, 2010 2:33 pm
- Location: Germany
- Contact:
Re: sorting files
Sure, just use a custom callback with Sort() that compares the values and tells Hollywood which entry comes first.
-
- Posts: 267
- Joined: Fri Feb 24, 2012 9:34 am
Re: sorting files
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?
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
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)
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)
-
- Posts: 267
- Joined: Fri Feb 24, 2012 9:34 am
Re: sorting files
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?
Error in line 6 (Unnamed1): Table field 1.71152e-316 was not initialized!
any idea about why this happens?
Re: sorting files
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()
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()
-
- Posts: 267
- Joined: Fri Feb 24, 2012 9:34 am
Re: sorting files
nice, it´s working now, thanks a lot