How to Sort by *alternating* numeric

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
PoLyGLoT
Posts: 105
Joined: Sat Jan 14, 2012 12:37 am

How to Sort by *alternating* numeric

Post by PoLyGLoT » Sun Sep 30, 2012 12:58 am

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.

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10058
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: How to Sort by *alternating* numeric

Post by FourthWorld » Sun Sep 30, 2012 4:55 am

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

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10356
Joined: Wed May 06, 2009 2:28 pm

Re: How to Sort by *alternating* numeric

Post by dunbarx » Sun Sep 30, 2012 6:27 am

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

PoLyGLoT
Posts: 105
Joined: Sat Jan 14, 2012 12:37 am

Re: How to Sort by *alternating* numeric

Post by PoLyGLoT » Sun Sep 30, 2012 3:12 pm

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:

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.

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7394
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: How to Sort by *alternating* numeric

Post by jacque » Sun Sep 30, 2012 10:23 pm

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:

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
They aren't really even/odd numbers but it was a convenient name for the variables.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

Post Reply