Hi,
I have the following field:
12,1
7,3
6,1
2,1
10,1
11,1
4,3
8,3
3,3
5,3
9,1
1,3
I want to sort it in an alternating numeric fashion, so that ITEM 2 goes (1,3,1,3,1,3,1,3,1,3....).
This has taken me hours and nothing seems to work. Your help would be much appreciated.
How to Sort by *alternating* numeric
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller
-
- VIP Livecode Opensource Backer
- Posts: 10058
- Joined: Sat Apr 08, 2006 7:05 am
- Contact:
Re: How to Sort by *alternating* numeric
sort lines of tMyVar by item 2 of each
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
Re: How to Sort by *alternating* numeric
Richard, I think he wants to sort by "alternate" values of the second item, not the second item itself.
The example was "1.3.1.3...."
I am not sure what this means, so I will ask.
What does this mean? You have a list with numbers in two items in each line. The second item is always either a 1 or a 3. The first item is any old number. So do you want to sort the second items numerically, giving a list sorted with all 1's in the second item place and then all 3's, which is what Richard's code would do, and then sort of shuffle these so that the firstMost "3" in the second item place is relocated to the second line? And then the nextMost "3" is relocated to the fourth line, and so forth? What happens if there is a different number of 1/3 pairs?
Or do you want to sort the numbers in the first item, giving an ascending list, and then reshuffle so that alternating 1/3 second item numbers are laid out as best as possible?
Can you tell us what you intend with this odd sorting thingie? I suspect there is a different way to do what you need.
Craig Newman
The example was "1.3.1.3...."
I am not sure what this means, so I will ask.
What does this mean? You have a list with numbers in two items in each line. The second item is always either a 1 or a 3. The first item is any old number. So do you want to sort the second items numerically, giving a list sorted with all 1's in the second item place and then all 3's, which is what Richard's code would do, and then sort of shuffle these so that the firstMost "3" in the second item place is relocated to the second line? And then the nextMost "3" is relocated to the fourth line, and so forth? What happens if there is a different number of 1/3 pairs?
Or do you want to sort the numbers in the first item, giving an ascending list, and then reshuffle so that alternating 1/3 second item numbers are laid out as best as possible?
Can you tell us what you intend with this odd sorting thingie? I suspect there is a different way to do what you need.
Craig Newman
Re: How to Sort by *alternating* numeric
Thanks for the comments.
The only condition I need to satisfy is that each next set of 2 lines has a 1 and 3 (and not a 1,1 or a 3,3). The idea is that once I've sorted in this manner, I'm going to be adding information into a variable 2 lines at a time, and the variable needs to have half 1's and half 3's.
I found a workaround for now:
This puts the items in temp so that they are alternating 1's and 3's, but I'd still like to know if it's possible to automatically use a sort command to alternate 1's and 3's.
The only condition I need to satisfy is that each next set of 2 lines has a 1 and 3 (and not a 1,1 or a 3,3). The idea is that once I've sorted in this manner, I'm going to be adding information into a variable 2 lines at a time, and the variable needs to have half 1's and half 3's.
I found a workaround for now:
Code: Select all
sort crittracker numeric by item 2 of each
put empty into temp
repeat with x = 1 to 6
put line x of crittracker & return after temp
put line (x+6) of crittracker & return after temp
end repeat
This puts the items in temp so that they are alternating 1's and 3's, but I'd still like to know if it's possible to automatically use a sort command to alternate 1's and 3's.
Re: How to Sort by *alternating* numeric
LiveCode allows sorting by a custom function. I didn't test on anything more than the example list you provided, but this seems to work:
They aren't really even/odd numbers but it was a convenient name for the variables.
Code: Select all
sort crittracker by getAlternates(item 2 of each)
local sLastEven,sLastOdd
function getAlternates pNum
if pNum = 1 then
add 1 to sLastEven
return sLastEven
else
add 1 to sLastOdd
return sLastOdd
end if
end getAlternates
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com
HyperActive Software | http://www.hyperactivesw.com