+
+let global_sort = {};
+
+function sort_by(th) {
+ let tr = th.parentElement;
+ let child_idx = 0;
+ for (let column_idx = 0; column_idx < tr.children.length; ++column_idx) {
+ let element = tr.children[column_idx];
+ if (element === th) {
+ ++child_idx; // Pad.
+ break;
+ }
+ if (element.hasAttribute('colspan')) {
+ child_idx += parseInt(element.getAttribute('colspan'));
+ } else {
+ ++child_idx;
+ }
+ }
+
+ global_sort = {};
+ let table = tr.parentElement;
+ for (let row_idx = 1; row_idx < table.children.length - 1; ++row_idx) { // Skip header and globals.
+ let row = table.children[row_idx];
+ let player = parseInt(row.dataset.player);
+ let value = row.children[child_idx].textContent;
+ global_sort[player] = value;
+ }
+}
+
+function get_sorted_players(players)
+{
+ let p = Object.entries(players);
+ if (global_sort.length !== 0) {
+ p.sort((a,b) => {
+ let ai = parseFloat(global_sort[a[0]]);
+ let bi = parseFloat(global_sort[b[0]]);
+ if (ai == ai && bi == bi) {
+ return bi - ai; // Reverse numeric.
+ } else if (global_sort[a[0]] < global_sort[b[0]]) {
+ return -1;
+ } else if (global_sort[a[0]] > global_sort[b[0]]) {
+ return 1;
+ } else {
+ return 0;
+ }
+ });
+ }
+ return p;
+}