Re: Sorting lists of strings and lists of lists in Applescript
Re: Sorting lists of strings and lists of lists in Applescript
- Subject: Re: Sorting lists of strings and lists of lists in Applescript
- From: Emmanuel LEVY <email@hidden>
- Date: Wed, 19 Mar 2014 11:51:36 +0100
Hi,
In case it helps, here is how you could "transpose" your lists to make them suitable for sortlist.
1. use printf to make your lists into a single string, one child list becoming one column,
2. use "paragraphs of" to split the list into its rows,
3. use "splittext" to split each row into a list,
4. use "items 1 thru -2" because we made a superfluous last empty line.
Which gives:
set random_list_of_lists to {{"adam", "male", 33}, {"penny", "female", 43}, {"prada", "genderless", 15}, {"swati", "female", 47}, {"teddy", "male", 33}, {"james", "male", 33}, {"Teddy", "male", 33}}
set newlist to items 1 thru -2 of (splittext (paragraphs of (printf ((join (change "." into "%s" in ((createarray (count random_list_of_lists)) as list of integer) with regexp) using tab) & return) parameters random_list_of_lists)) using tab)
-- {{"adam", "penny", "prada", "swati", "teddy", "james", "Teddy"}, {"male", "female", "genderless", "female", "male", "male", "male"}, {"33", "43", "15", "47", "33", "33", "33"}}
sortlist newlist comparison 1 with respect to 2 with ascending
-- {{"penny", "swati", "prada", "adam", "teddy", "james", "Teddy"}, {"female", "female", "genderless", "male", "male", "male", "male"}, {"43", "47", "15", "33", "33", "33", "33"}}
This includes a clumsy step to build the formatting string for printf:
(join (change "." into "%s" in ((createarray (count random_list_of_lists)) as list of integer) with regexp) using tab) & return
just builds the string: "%s [ tab ] %s [ tab ] etc.
I think that "with respect to" does not want a list.
If you have a problem with Teddy vs teddy, note that lowercase works on lists as well as strings.
Best,
Emmanuel
On Mar 19, 2014, at 1:51 AM, Alastair Leith wrote:
> Hi Ted
>
> That helps so much as I see how it works now, except I was hoping for sorting in the other 'dimension' if I can use that turn of phrase.
>
> I was thinking about a list of lists where each child list is an individual record, from a table of similar records.
>
> So I want to sort by gender, then age, then name for example:
> set random_list_of_lists to {{"adam", "male", 33}, ¬
> {"penny", "female", 43}, ¬
> {"prada", "genderless", 15}, ¬
> {"swati", "female", 47}, ¬
> {"teddy", "male", 33}, ¬
> {"james", "male", 33}, ¬
> {"Teddy", "male", 33}}
>
> set sorted_list_of_lists to sortlist random_list_of_lists ¬
> ascending true ¬
> remove duplicates true ¬ -- wont work on teddy/Teddy
> comparison 1 ¬
> with respect to {2,3,1} -- wrong interpretation!
>
> I could run transpose the lists into {{"adam","penny",…}, {"male","female",…}, {33, 43,…}} I guess. But is there a way to do it if I have lists of records like this, or even as lists with property labels (hashes) {name: "adam, gender: "male", age: 33}, etc etc
>
>
>
>
> On 19 Mar 2014, at 11:01 am, Ted Wrigley <email@hidden> wrote:
>
>>
>> On Mar 18, 2014, at 12:00 PM, email@hidden wrote:
>>
>>> From: Alastair Leith <email@hidden>
>>> Subject: Sorting lists of strings and lists of lists in Applescript
>>> Date: March 17, 2014 at 10:07:16 PM PDT
>>> To: asu <email@hidden>
>>>
>>> The dictionary description suggests this command will also sort a list of lists using the "with respect to" argument by prioritising the criteria with integers (presumably like a table sort in a spreadsheet app but no example or specification is provided).
>>
>> You use this to sort a list of lists. in this form it’s a parallel sort: the command sorts the list at the given ‘with respect to’ index and rearranges the other lists in exactly the same way. examples:
>>
>> set l1 to {{"r", "q", "f", "c"}, {"a", "n", "z", "b"}, {"t", "a", "l", "q"}, {"p", "p", "z", "l"}}
>>
>> sortlist l1
>> — sorts list 1 (by default) and rearranges the remaining lists in parallel
>> --{{"c", "f", "q", "r"}, {"a", "b", "n", "z"}, {"a", "l", "q", "t"}, {"l", "p", "p", "z"}}
>>
>> sortlist l1 with respect to 2
>> — sorts list 2 and sorts the remaining lists in parallel
>> --{{"r", "c", "q", "f"}, {"a", "b", "n", "z"}, {"t", "q", "a", "l"}, {"p", "l", "p", "z"}}
>>
>> sortlist l1 with respect to {4, 3} without ascending
>> — sorts list 4 (descending) first (and the other lists in parallel) then resorts items 2 and 3 (where list 4 has matching P’s) by the descending sort order of those items in list 3
>> --{{"f", "r", "q", "c"}, {"z", "a", "n", "b"}, {"l", "t", "a", "q"}, {"z", "p", "p", "l"}}
>>
>> sortlist l1 with respect to {4, 3} ascending {true, false}
>> — same as above except the initial sort is descending and the sub-sort is ascending.
>> --{{"c", "r", "q", "f"}, {"b", "a", "n", "z"}, {"q", "t", "a", "l"}, {"l", "p", "p", "z"}}
>>
>> Hope that helps...
>> _______________________________________________
>> Do not post admin requests to the list. They will be ignored.
>> AppleScript-Users mailing list (email@hidden)
>> Help/Unsubscribe/Update your Subscription:
>> Archives: http://lists.apple.com/archives/applescript-users
>>
>> This email sent to email@hidden
>
> _______________________________________________
> Do not post admin requests to the list. They will be ignored.
> AppleScript-Users mailing list (email@hidden)
> Help/Unsubscribe/Update your Subscription:
> Archives: http://lists.apple.com/archives/applescript-users
>
> This email sent to email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users
This email sent to email@hidden