Ranking methods: Difference between revisions
m
→{{header|AppleScript}}: New sort handler URL, tidy-up.
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
m (→{{header|AppleScript}}: New sort handler URL, tidy-up.) |
||
Line 184:
=={{header|AppleScript}}==
<syntaxhighlight lang="applescript">use AppleScript version "2.
use sorter : script ¬
▲ The five ranking methods are implemented here as script objects sharing inherited code rather than as simple
▲use AppleScript version "2.4" -- Mac OS 10.11 (Yosemite) or later (for these 'use' commands).
▲use sorter : script "Custom Iterative Ternary Merge Sort" -- <https://macscripter.net/viewtopic.php?pid=194430#p194430>
script standardRanking
-- Properties and handlers inherited or overridden by the other script objects.
-- The '
-- 'me' and 'my'
property results : missing value
property startIndex : 1
property endIndex : a reference to
property step : 1
property currentRank : missing value
Line 207 ⟶ 203:
on resultsFrom(theScores)
copy theScores to my results
tell sorter to sort(my results, my startIndex, my endIndex, {comparer:me})
set my currentScore to
set my currentRank to my startIndex's contents
repeat with i from (my startIndex) to (my endIndex) by (my step)
my rankResult(i)
end repeat
▲ set localVariable to my results
set my results to missing value
return
end resultsFrom
-- Comparison handler used by the sort
on isGreater(a, b)
if (a's score
return ((a's score
end isGreater
--
on rankResult(i)
set thisResult to my results's item i
set thisScore to thisResult's score
if (thisScore is not currentScore) then
Line 238 ⟶ 228:
set my currentScore to thisScore
end if
set my results's item i
end rankResult
end script
script modifiedRanking
property parent : standardRanking
property startIndex : a reference to
property endIndex : 1
property step : -1
Line 253 ⟶ 243:
on rankResult(i)
set thisResult to my results's item i
set thisScore to thisResult's score
if (thisScore is not my currentScore) then
Line 259 ⟶ 249:
set my currentScore to thisScore
end if
set my results's item i
end rankResult
end script
Line 267 ⟶ 257:
on rankResult(i)
set my results's item i
end rankResult
end script
Line 275 ⟶ 265:
on rankResult(i)
set thisResult to my results's item i
set thisScore to thisResult's score
if (thisScore is not my currentScore) then
-- The average of any run of consecutive integers is that of the first and last.
set average to (i - 1 + (my currentRank)) / 2
repeat with j from (my currentRank) to (i - 1)
set
end repeat
set my currentRank to i
set my currentScore to thisScore
end if
set my results's item i
end rankResult
end script
Line 295 ⟶ 284:
set rankings to {type}
repeat with thisResult in theResults
set end of rankings to (thisResult's rank as text) & tab &
thisResult's competitor & " (" & thisResult's score & ")"
end repeat
return
end formatRankings
on join(lst, delim)
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to
set
set AppleScript's text item delimiters to astid
return txt
end join
local theScores, output
set theScores to {{score:44, competitor:"Solomon"}, {score:42, competitor:"Jason
{score:
{score:41, competitor:"Barry"}, {score:39, competitor:"Stephen"}}
set output to {
}
return
{{output}}
|