fork download
  1. /*
  2.   TinyTableV3 ソート実装の修正案1 (Google Chrome 7 でソートされないバグを修正)
  3. */
  4.  
  5. function cp (string1, string2) {
  6. var result, number1, number2;
  7.  
  8. string1 = String(string1).toLowerCase().replace(/^\s+|\s+$/g, '');
  9. string2 = String(string2).toLowerCase().replace(/^\s+|\s+$/g, '');
  10.  
  11. if (string1 === string2) {
  12. return 0;
  13. } else if (string1 > string2) {
  14. result = 1;
  15. } else {
  16. result = -1;
  17. }
  18.  
  19. number1 = parseFloat(string1.replace(/[,$]/g, ''));
  20. number2 = parseFloat(string2.replace(/[,$]/g, ''));
  21.  
  22. if (!isNaN(number1) && !isNaN(number2) && number1 !== number2) {
  23. if (number1 > number2) {
  24. return 1;
  25. } else if (number1 < number2) {
  26. return -1;
  27. }
  28. }
  29.  
  30. number1 = Date.parse(string1);
  31. number2 = Date.parse(string2);
  32.  
  33. if (!isNaN(number1) && !isNaN(number2) && number1 !== number2) {
  34. if (number1 > number2) {
  35. return 1;
  36. } else if (number1 < number2) {
  37. return -1;
  38. }
  39. }
  40. return result;
  41. }
  42.  
  43. function format_array (array) {
  44. return '["' + array.join('", "') + '"]';
  45. }
  46.  
  47. print(format_array(['11f', '11e', '13a', '12b'].sort(cp)));
  48. print(format_array(['11/3', '12/c', '12/a', '11/5', '11/2'].sort(cp)));
  49. print(format_array(['12/01/1981', '02/14/1990', '03/29/1970', '12/02/1962'].sort(cp)));
  50. print(format_array(['41%', '55%', '6%', '81%'].sort(cp)));
  51. print(format_array(['-1', '-10', '1', '5', '-7', '6'].sort(cp)));
  52. print(format_array(['February 25, 2001', 'April 30, 2003', 'July 23, 2005', 'June 22, 2004', 'April 27, 2002'].sort(cp)));
  53. print(format_array(['a1b', 'a1a', 'a', 'a2', 'a0', 'a20', 'a10', 'a1'].sort(cp)));
  54. print(format_array(['rfc1.txt', 'rfc2086.txt', 'rfc822.txt'].sort(cp)));
Success #stdin #stdout 0.3s 214656KB
stdin
Standard input is empty
stdout
["11e", "11f", "12b", "13a"]
["11/2", "11/3", "11/5", "12/a", "12/c"]
["02/14/1990", "03/29/1970", "12/02/1962", "12/01/1981"]
["6%", "41%", "55%", "81%"]
["-10", "-7", "-1", "1", "5", "6"]
["February 25, 2001", "April 27, 2002", "April 30, 2003", "June 22, 2004", "July 23, 2005"]
["a", "a0", "a1", "a10", "a1a", "a1b", "a2", "a20"]
["rfc1.txt", "rfc2086.txt", "rfc822.txt"]