Concatenation and 2 variables

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
Wally
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 46
Joined: Sun Jun 15, 2008 4:51 pm

Concatenation and 2 variables

Post by Wally »

This statement works as expected:
sort tList descending numeric by item 7 of each
tList is a variable with content from a revQueryDatabase

This statement fails: (statstosort is a variable with item 2 or item 18 etc)
sort tList descending numeric by statstosort of each

I've tried every combo of ampersands, quotes for the variables tList and statstosort until my eyeballs pop out. Gotta be easy - help!
bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4219
Joined: Sun Jan 07, 2007 9:12 pm

Re: Concatenation and 2 variables

Post by bn »

Hi Wally,

if I understand you correctly then
you want to put the sort parameter into a variable?
You don't want to do a sort by 2 parameters?

if you want to use a variable that indicates which item to sort on then this may help

Code: Select all

on mouseUp
   put field 1 into tList
   put 2 into statstosort -- determines which item will be used for sorting
   sort lines of tList descending numeric by item statstosort of each
   put tList into field 2
end mouseUp
Kind regards
Bernd
bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4219
Joined: Sun Jan 07, 2007 9:12 pm

Re: Concatenation and 2 variables

Post by bn »

Hi Wally,

on the other hand if you want to sort by 2 variables then you can do this

Code: Select all

on mouseUp
   put field 1 into tData
   sort tData descending numeric  by item 2 of each
   sort tData descending numeric by item 1 of each 
   put tData into field 2
end mouseUp
As the dictionary says: sort is a stable sort. So sort on the least important item first then on the more important one.

e.g.
you want to sort

2,10
4,3
1,6
2,2
4,10

by sorting with above code descending numeric you will get

4,10
4,3
2,10
2,2
1,6

I did not get the concatenation to work, the second item did not sort numeric but by ascii value. Maybe I did something wrong

Kind regards
Bernd
Bernard
Posts: 351
Joined: Sat Apr 08, 2006 10:14 pm

Re: Concatenation and 2 variables

Post by Bernard »

If I understand Wally correctly, he wants the item by which the sort is done to be specified at run-time.

One can use a custom sort function for this.

Code: Select all

on mouseUp
   put fld "ListOfItems" into tData -- where fld 1 contains lines with 18+ items
   put fld "ItemToSortBy" into tItemNumber  -- assume 18 as in the description given by Wally
   sort tData ascending by MyCustomSort(each, tItemNumber)
   put tData
end mouseUp

function MyCustomSort pLine, pItemNumber
  return pItemNumber of pLine
end MyCustomSort
See the "Tip" and Oliver's example here: http://docs.runrev.com/Command/sort-container
Wally
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 46
Joined: Sun Jun 15, 2008 4:51 pm

Re: Concatenation and 2 variables

Post by Wally »

Bernard,

Thank you so much! Your tips have helped me solve the problem. The livecode community is incredible!

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

Re: Concatenation and 2 variables

Post by dunbarx »

Bernard.

You are so correct in pointing out the power of custom functions in sorts. There was a typo, I think, in your post, though.

Code: Select all

on mouseUp
   put "" into fld "tResult"
   put fld "ListOfItems" into tData -- where fld 1 contains lines with 18+ items
   put fld "ItemToSortBy" into tItemNumber  -- assume 18 as in the description given by Wally

   sort lines of tData ascending by MyCustomSort(each, tItemNumber)
   put tData into fld "tResult"
end mouseUp

function MyCustomSort pLine, pItemNumber
  return item pItemNumber of pLine 
end MyCustomSort
The function itself needed just a bit of tweaking.

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

Re: Concatenation and 2 variables

Post by jacque »

You can do it without a custom function too:

Code: Select all

put fld "ListOfItems" into tData
put fld "ItemToSortBy" into tItemNumber
sort lines of tData by item tItemNumber of each
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com
dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10501
Joined: Wed May 06, 2009 2:28 pm

Re: Concatenation and 2 variables

Post by dunbarx »

Jacque.

True enough.

The dictionary, however, has a simple user note (by Oliver) of sorting with a custom function. Shows how a little embedded preProcessing can go a long way:

Code: Select all

sort lines of field "Prices" descending numeric by stripCurrency(each)

private function stripCurrency pSearchItem
   if char 1 of pSearchItem is "$" then
      return char 2 to -1 of pSearchItem
   else
      return pSearchItem
   end if
end stripCurrency
This demonstrates clearly how "each" is a local variable, and how code can be made robust and modular.

So much fun.

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

Re: Concatenation and 2 variables

Post by jacque »

Craig, right, and now I see that Bernd had already mentioned the one-liner. So I was late to the party again.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com
Post Reply