AppleScript: How do I sort a list of sentences/paragraphs with an assigned number prefix?

49 Views Asked by At

I've been having difficulty finding any question similar to my issue, so I made an account just to ask.

So let's say I have a list of 10 random questions:

set questionList to {
    "10. If you could have any superpower, what would it be and why?",
    "1. What’s the most adventurous thing you’ve ever done?",
    "3. If you could travel back in time, which historical event would you want to witness?",
    "4. What’s your favorite book or movie, and why does it resonate with you?",
    "7. If you were stranded on a deserted island, what three items would you want to have with you?",
    "2. What’s a skill or hobby you’ve always wanted to learn but haven’t yet?",
    "8. If you could meet any famous person (dead or alive), who would it be and what would you ask them?",
    "5. What’s the best piece of advice you’ve ever received?",
    "9. If you could instantly master any musical instrument, which one would you choose?",
    "6. What’s a place you’ve never been to but would love to visit someday?"
}

How would I sort this into a new list numerically?

3

There are 3 best solutions below

0
vadian On

It's quite cumbersome to sort arbitrary items in AppleScript.
But it's pretty easy in AppleScriptObjC

use AppleScript version "2.5"
use framework "Foundation"
use scripting additions

set questionList to {"10. If you could have any superpower, what would it be and why?", "1. What’s the most adventurous thing you’ve ever done?", "3. If you could travel back in time, which historical event would you want to witness?", "4. What’s your favorite book or movie, and why does it resonate with you?", "7. If you were stranded on a deserted island, what three items would you want to have with you?", "2. What’s a skill or hobby you’ve always wanted to learn but haven’t yet?", "8. If you could meet any famous person (dead or alive), who would it be and what would you ask them?", "5. What’s the best piece of advice you’ve ever received?", "9. If you could instantly master any musical instrument, which one would you choose?", "6. What’s a place you’ve never been to but would love to visit someday?"}
set array to current application's NSArray's arrayWithArray:questionList
set sortedQuestionList to (array's sortedArrayUsingSelector:"localizedStandardCompare:") as list

localizedStandardCompare considers also numeric values.

0
Mockman On

Here is a bubble sort approach (at least as I understand it). If your list has a billion items, then it is probably inefficient (and you have other problems) but for a short list its performance should be fine.

As each item in your list is numbered, sorting is more complicated. To address that, I create a second list consisting of each entry's opening characters as an integer — whatever characters are before the first '. '. This is what the sorting is based on.

  • If your list has complex numbering then it will likely fail, (eg 1.2, 1.1, 2.1).
  • If your list has no numbering at all, then comment out the section above the looping as well as any 'nar' command, then uncomment the if item i of ar… line.
set ar to {"10. If you could have any superpower, what would it be and why?", ¬
    "1. What’s the most adventurous thing you’ve ever done?", ¬
    "3. If you could travel back in time, which historical event would you want to witness?", ¬
    "4. What’s your favorite book or movie, and why does it resonate with you?", ¬
    "7. If you were stranded on a deserted island, what three items would you want to have with you?", ¬
    "2. What’s a skill or hobby you’ve always wanted to learn but haven’t yet?", ¬
    "8. If you could meet any famous person (dead or alive), who would it be and what would you ask them?", ¬
    "5. What’s the best piece of advice you’ve ever received?", ¬
    "9. If you could instantly master any musical instrument, which one would you choose?", ¬
    "6. What’s a place you’ve never been to but would love to visit someday?"}

-- corresponding list of numbers -- remove (or comment) if list items are not 'numbered'
set nar to {} -- numbers of ar, eg 10, 1, 3…
set text item delimiters to ". "
repeat with x in ar
    set end of nar to text item 1 of x as integer
end repeat
--> {10, 1, 3, 4, 7, 2, 8, 5, 9, 6}


set n to length of ar -- how many items in list
repeat with i from 1 to (n - 1)
    repeat with j from (i + 1) to n
        if item i of nar is greater than item j of nar then -- use if entries are numbered
            -- if item i of ar is greater than item j of ar then -- use if entries are unnumbered

            set t to item j of ar -- temporary - lower of strings
            set nt to item j of nar -- temporary - lower of integers
            
            set item j of ar to item i of ar -- move higher string towards bottom
            set item j of nar to item i of nar
            
            set item i of ar to t -- move lower string towards top
            set item i of nar to nt
            -- log nar -- order after each pass
        end if
    end repeat
end repeat

ar
1
jweaks On

Applescript is not great at text manipulations. For this kind of work, I use a one-line shell script command. In this instance, I'd use sort(). You just convert your AppleScript list into a string a paragraphs using the linefeed character instead of paragraph marker, and then feed it into sort().

set questionList to {"10. If you could have any superpower, what would it be and why?", "1. What’s the most adventurous thing you’ve ever done?", "3. If you could travel back in time, which historical event would you want to witness?", "4. What’s your favorite book or movie, and why does it resonate with you?", "7. If you were stranded on a deserted island, what three items would you want to have with you?", "2. What’s a skill or hobby you’ve always wanted to learn but haven’t yet?", "8. If you could meet any famous person (dead or alive), who would it be and what would you ask them?", "5. What’s the best piece of advice you’ve ever received?", "9. If you could instantly master any musical instrument, which one would you choose?", "6. What’s a place you’ve never been to but would love to visit someday?"}
set AppleScript's text item delimiters to {ASCII character 10} -- must use linefeed
set myString to (questionList as string)
set newString to do shell script "echo " & quoted form of myString & " | sort -f -V"
set newList to (paragraphs of newString)
set AppleScript's text item delimiters to ""
return newList