Concatenation and 2 variables
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller
Concatenation and 2 variables
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!
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
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
Kind regards
Bernd
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
Bernd
Re: Concatenation and 2 variables
Hi Wally,
on the other hand if you want to sort by 2 variables then you can do this
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
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
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
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.
See the "Tip" and Oliver's example here: http://docs.runrev.com/Command/sort-container
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
Re: Concatenation and 2 variables
Bernard,
Thank you so much! Your tips have helped me solve the problem. The livecode community is incredible!
Wally
Thank you so much! Your tips have helped me solve the problem. The livecode community is incredible!
Wally
Re: Concatenation and 2 variables
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.
The function itself needed just a bit of tweaking.
Craig Newman
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
Craig Newman
Re: Concatenation and 2 variables
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
HyperActive Software | http://www.hyperactivesw.com
Re: Concatenation and 2 variables
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:
This demonstrates clearly how "each" is a local variable, and how code can be made robust and modular.
So much fun.
Craig
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
So much fun.
Craig
Re: Concatenation and 2 variables
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
HyperActive Software | http://www.hyperactivesw.com