Re: Sort value pairs in a list by integer
Re: Sort value pairs in a list by integer
- Subject: Re: Sort value pairs in a list by integer
- From: Deivy Petrescu <email@hidden>
- Date: Tue, 1 Jul 2003 18:43:52 -0400
On Tuesday, Jul 1, 2003, at 05:21 US/Eastern, Joseph Weaks wrote:
I can't get my head around this. I have a list that might contain
between 5 to 10 pairs of values:
set foobar to {"foo", 15, "bar", 6, "boo", 2, "far", 19}
The numbers "go with" the preceding string and I want to return a list
sorted in descending order by the integer value. ie. the result should
be
sortIt(foobar)
-- {"far", 19, "foo", 15, "bar", 6, "boo", 2}
Any help is appreciated.
Thanks,
Joe Weaks
Joe, tweaking a bit this stupendous script by Emmanuel Levy (think
Smile) you have this:
<script>
on sort(lista) -- this code is here with permission from Emmanuel LC)vy
and Satimage.
if length of lista b $ 2 then return lista
set y to {item 1 of lista, item 2 of lista}
set x to item 2 of lista
set lista to rest of (rest of lista)
set lista1 to {}
set lista2 to {}
set lght to length of lista
repeat with k from 2 to lght by 2
set i to contents of item k of lista
set j to contents of item (k - 1) of lista
if x > i then
set lista2 to lista2 & {j, i}
else
set lista1 to lista1 & {j, i}
end if
end repeat
return sort(lista1) & y & sort(lista2)
end sort
set foobar to {"foo", 15, "bar", 6, "boo", 2, "far", 19}
sort(foobar)
----> {"far", 19, "foo", 15, "bar", 6, "boo", 2}
</script>
you do not need to thank him... just download and use Smile :)
Regards
Deivy Petrescu
http://www.dicas.com/
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.