Different sort of sort
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller
Different sort of sort
I have lines in a field...
H,2.1
R,1.2
B,4.2
L,3.1
O,7.7
E,3.4
I want to sort them in order R,B,H,L,O,E.
How can I sort this way?
H,2.1
R,1.2
B,4.2
L,3.1
O,7.7
E,3.4
I want to sort them in order R,B,H,L,O,E.
How can I sort this way?
Is age an excuse? Eighty-four and counting.
Programming powered by coffee.
Programming powered by coffee.
Re: Different sort of sort
You need a custom sort function for something like this. Assuming a field that contains the data:
Code: Select all
on customSort
put fld 1 into tList -- you could use a variable here instead
put "R,B,H,L,O,E" into tOrderList
split tOrderList by comma
sort lines of tList by sortIt(each,tOrderList)
-- do something with the sorted list here
end customSort
function sortit tLine,tOrderList
repeat for each key k in tOrderList
if tOrderList[k] = item 1 of tLine then
return k
end if
end repeat
end sortit
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com
HyperActive Software | http://www.hyperactivesw.com
Re: Different sort of sort
Hi, RossG.
You could sort the field using an array:
-- Dick
You could sort the field using an array:
Code: Select all
repeat for each item k in "R,B,H,L,O,E"
add 1 to i
put i into a[ k ]
end repeat
sort fld 1 by a[ item 1 of each ]
Re: Different sort of sort
This is an interesting problem:
What to do with lines, that have something else as a first item (missing data/empty, typo)?
Here is a variant that includes this aspect.
[The 'numeric' is only needed in case you have more than 9 items in your orderList]
What to do with lines, that have something else as a first item (missing data/empty, typo)?
Here is a variant that includes this aspect.
Code: Select all
local orderList = "R,B,H,L,O,E"
sort myVar numeric by itemoffset(item 1 of each, orderList)
shiftLock happens
Re: Different sort of sort
@ Jacque, Dick.
Nice.
@Hermann.
Really nice.
All three of these should be combined as three instances of a solution to a problem, in a lesson in their own right.
Craig
Nice.
@Hermann.
Really nice.
All three of these should be combined as three instances of a solution to a problem, in a lesson in their own right.
Craig
Re: Different sort of sort
What I would have said if I hadn't been so admiring of thesedunbarx wrote:@ Jacque, Dick.
Nice.
@Hermann.
Really nice.
All three of these should be combined as three instances of a solution to a problem, in a lesson in their own right.
Craig
solutions that I was left "speechless". Three cheers and a cigar to each. Thank you.
Is age an excuse? Eighty-four and counting.
Programming powered by coffee.
Programming powered by coffee.