Sorting Method / Toplists

Requirements: Skript and Skript-Mirror 2.0
Tested on: 1.8.8, 1.13.2, 1.14.4
Difficulty: Semi-Basic/Complex

This is a small tutorial on how to create a toplist with Skript based on a couple expressions made in skript-mirror.

The skript-mirror expressions / sorting algorithm:
import:
java.util.ArrayList
java.util.Collections
java.util.Map$Entry
ch.njol.skript.variables.Variables

expression replace values %strings% with %strings% in %string%:
get:
set {_string} to expression-3
loop expressions-1:
add 1 to {_index}
replace all "%loop-value%" with "%{_index}th element of expressions-2%" in {_string}
return {_string}

plural expression [the] (1¦(highest|top)|2¦(lowest|last)) %integer% values of %objects% [formatted] as %string%:
get:
set {_list} to new ArrayList(Variables.getVariable(raw expressions-2.getName().toString(event), event, raw expressions-2.isLocal()).entrySet())
{_list}.sort(Entry.comparingByValue())
if parse mark = 1:
Collections.reverse({_list})
loop ...{_list}:
"%loop-value.getValue()%" is not "0"
add 1 to {_index}
set {_sorted::%{_index}%} to replace values "@place", "@index" and "@value" with "%{_index}%", "%loop-value.getKey()%" and "%loop-value.getValue()%" in expression-3
{_index} = expression-1
stop loop
return {_sorted::*}

These expressions will allow us to make toplists. The syntax is not that hard to use and it uses a very efficient sorting algorithm. It is much faster than anything you can really make with vanilla Skript. I did some testing and this algorithm can sort up to 50,000 values in one second!

You may be thinking, what are "@place" and "@index" and "@value" in this list. @place represents what place the value is in, for example, first place or second place. @index represents the key. For example, in a toplist, the key would be the player name or UUID. @value represents the value. For example, if a toplist was based on kills, it would be the amount of kills the player has.

Note: it is recommended to make an auto-updator for leaderboards and not update it every time they use a command. Player's that spam a command like this could crash the server! This is just an example.
Example toplist #1 ( Top Kills based on {Kills::*} ):
command /topkillers:
trigger:
set {_topKillers::*} to the highest (size of {Kills::*}) values of {Kills::*} as "&e@place. &b@index &7- &6@value"
message "&eTop Killers"
loop {_topkillers::*}:
message loop-value
#if loop-index is "10":
# stop loop

This toplist example will sort everything in {Kills::*} from highest to lowest.

Note: it is recommended to make an auto-updator for leaderboards and not update it every time they use a command. Player's that spam a command like this could crash the server! This is just an example.
Example toplist #2 ( Top Kills based on {Kills::*}, 10 displayed per page )
command /topkillers [<integer=1>]:
trigger:

# Example amount to show per page. In this example, its 10
set {_showPerPage} to 10
set {_toSortTo} to arg 1 * {_showPerPage}

set {_topKillers::*} to the highest {_toSortTo} values of {Kills::*} as "&e@place. &b@index &7- Kills: &6@value"
message "&eTop Killers Page %arg 1%"
loop {_topkillers::*}:
if ("%loop-index%" parsed as integer) is between {_toSortTo} - ({_showPerPage} - 1) and {_toSortTo}:
message loop-value

This toplist example will set values until a certain point (more efficient than sorting all unless you need to). This toplist uses a page system.

Picture Example of toplist #2:

I could add a few more examples but I should have given you enough info to get started on your own. Good luck with your toplists!

Credits: EWS (I didn't originally make this method of sorting. I edited the original version from Mirrorutils and also showed how to use it.)