Previous | Index | Next |
List manipulation methods
sortedArrayFrom:stableSort:usingSelector:target:
Sorts a list using the sort algorithm you provide. The selector will be used as the sorting comparator.
'stableSort' should be true for a stable sort.
The selector needs to take two parameters, which are the two values being compared. It should return 1 if the first value is the greater, -1 if it's the lesser, and 0 if they are the same. So you might pass something like: "compareThis:withThis:", and your script would then include a handler "on compareThis:x withThis:y".
For target, pass the constant me.
The calling of methods back-and-forth means this can be a pretty slow way to sort, especially if the objects are bridged.
+ (NSArray *)sortedArrayFrom:(NSArray *)listOrArray stableSort:(BOOL)stableSortFlag usingSelector:(SEL)selectorString target:(NSObject *)target
listOrArray = list or array to sort
stableSortFlag = boolean value
selectorString = string of the form “compareThis:withThat:”
target = usually me
The sorted array
Version 1.0.0
use scripting additions
use framework "Foundation"
use script "BridgePlus"
load framework
set listOrArray to {"one", "two", "three", "four", "five"}
set theResult to current application's SMSForder's sortedArrayFrom:listOrArray stableSort:true usingSelector:"compareThis:withThis:" target:me
ASify from theResult
--> {"one", "three", "five", "two", "four"}
theResult as list
--> {"one", "three", "five", "two", "four"}
on compareThis:x withThis:y
-- sort by last character of string
set x to x as text
set y to y as text
if last character of x > last character of y then
return 1
else if last character of x < last character of y then
return -1
else
return 0
end if
end compareThis:withThis: