Sort Uploads Alphabetically

Sort user uploads alphabetically

  1. // ==UserScript==
  2. // @name Sort Uploads Alphabetically
  3. // @namespace pxgamer
  4. // @version 0.2
  5. // @description Sort user uploads alphabetically
  6. // @author pxgamer
  7. // @include *kat.cr/user/*/uploads/
  8. // @grant none
  9. // ==/UserScript==
  10.  
  11. (function() {
  12. 'use strict';
  13.  
  14. var rows = [];
  15.  
  16. $('tr.firstr th.width100perc.nopad').html(
  17. '<a class="sortAlpha">torrent name</a>'
  18. );
  19.  
  20. $('.data tr[id^="torrent_"]').each(function() {
  21. var title = $('.cellMainLink', $(this)).text();
  22. var html = $(this).html();
  23. rows.push({"title":title, "html":html});
  24. });
  25.  
  26. $('.sortAlpha').on('click', function() {
  27. var sortName = 'title';
  28. var sortType = 'desc';
  29. sortTable(sortName, sortType);
  30. });
  31.  
  32. function sortByKey(array, key) {
  33. return array.sort(function(a, b) {
  34. var x = a[key];
  35. var y = b[key];
  36. if (typeof x == "string") {
  37. x = x.toLowerCase();
  38. y = y.toLowerCase();
  39. }
  40. return ((x < y) ? -1 : ((x > y) ? 1 : 0));
  41. });
  42. }
  43.  
  44. function sortTable(sortName, sortType) {
  45. sortByKey(rows, sortName);
  46. if (!sortType) {
  47. rows.reverse();
  48. }
  49. $('.data tr[id^="torrent_"]').remove();
  50. for (var i=0;i<rows.length;i++) {
  51. $('.data').append('<tr id="torrent_'+i+'">'+rows[i].html+'</tr>');
  52. }
  53.  
  54. }
  55. })();