Applescript:出于好奇,如何对具有指定数字前缀的句子/段落列表进行排序?

问题描述 投票:0回答:1

我一直很难找到与我的问题类似的问题,所以我注册了一个帐户只是为了提问。

假设我有 10 个随机问题的列表:

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?"}

我如何将其按数字排序到新列表中?

sorting applescript
1个回答
1
投票

在AppleScript中对任意项目进行排序是相当麻烦的。
但在 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
也考虑数值。

© www.soinside.com 2019 - 2024. All rights reserved.