Page 1 of 1
Concatenation and 2 variables
Posted: Tue Nov 06, 2012 8:52 pm
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!
Re: Concatenation and 2 variables
Posted: Tue Nov 06, 2012 10:26 pm
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
Re: Concatenation and 2 variables
Posted: Tue Nov 06, 2012 10:58 pm
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
Re: Concatenation and 2 variables
Posted: Wed Nov 07, 2012 12:53 am
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
Re: Concatenation and 2 variables
Posted: Wed Nov 07, 2012 2:25 am
by Wally
Bernard,
Thank you so much! Your tips have helped me solve the problem. The livecode community is incredible!
Wally
Re: Concatenation and 2 variables
Posted: Wed Nov 07, 2012 4:33 am
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
Re: Concatenation and 2 variables
Posted: Wed Nov 07, 2012 6:37 pm
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
Re: Concatenation and 2 variables
Posted: Thu Nov 08, 2012 2:31 am
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
Re: Concatenation and 2 variables
Posted: Fri Nov 09, 2012 7:58 pm
by jacque
Craig, right, and now I see that Bernd had already mentioned the one-liner. So I was late to the party again.