You can make a difference in the Apple Support Community!

When you sign up with your Apple Account, you can provide valuable feedback to other community members by upvoting helpful replies and User Tips.

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Can I sort a list of items based on another list of items?

I've looked around the forums but nothing seems to be close to what I am trying to do.


I have a list of items called petList


{"Garfield", "Pluto", "Snoopy"}


I have a list called petOrder

{"Snoopy", "Tweety", "Pluto", "Top Cat", Garfield"}

I want the items in petList to sort according to petOrder and end up like this:


{"Snoopy", "Pluto", "Garfield"}


Is this possible just using Applescript?

Mac Pro, OS X Yosemite (10.10.4), 16 GB RAM, 2x2.4 GHz Quad-Core Xeon

Posted on Jul 4, 2015 9:46 PM

Reply
5 replies

Jul 4, 2015 11:41 PM in response to tikilab

The following script seems to do what you are asking for, assuming that every item in petList also belongs to petOrder:


set petList to {"Garfield", "Pluto", "Snoopy"}

set petOrder to {"Snoopy", "Tweety", "Pluto", "Top Cat", "Garfield"}

set newList to {}


repeat with k from 1 to (countpetOrder)

if itemk of petOrder is in petList then copy itemk of petOrder to the end of newList

end repeat


newList--> {"Snoopy", "Pluto", "Garfield"}

Can I sort a list of items based on another list of items?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.