You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

125 lines
3.3 KiB

4 years ago
  1. [Build]: http://img.shields.io/travis/litejs/natural-compare-lite.png
  2. [Coverage]: http://img.shields.io/coveralls/litejs/natural-compare-lite.png
  3. [1]: https://travis-ci.org/litejs/natural-compare-lite
  4. [2]: https://coveralls.io/r/litejs/natural-compare-lite
  5. [npm package]: https://npmjs.org/package/natural-compare-lite
  6. [GitHub repo]: https://github.com/litejs/natural-compare-lite
  7. @version 1.4.0
  8. @date 2015-10-26
  9. @stability 3 - Stable
  10. Natural Compare – [![Build][]][1] [![Coverage][]][2]
  11. ===============
  12. Compare strings containing a mix of letters and numbers
  13. in the way a human being would in sort order.
  14. This is described as a "natural ordering".
  15. ```text
  16. Standard sorting: Natural order sorting:
  17. img1.png img1.png
  18. img10.png img2.png
  19. img12.png img10.png
  20. img2.png img12.png
  21. ```
  22. String.naturalCompare returns a number indicating
  23. whether a reference string comes before or after or is the same
  24. as the given string in sort order.
  25. Use it with builtin sort() function.
  26. ### Installation
  27. - In browser
  28. ```html
  29. <script src=min.natural-compare.js></script>
  30. ```
  31. - In node.js: `npm install natural-compare-lite`
  32. ```javascript
  33. require("natural-compare-lite")
  34. ```
  35. ### Usage
  36. ```javascript
  37. // Simple case sensitive example
  38. var a = ["z1.doc", "z10.doc", "z17.doc", "z2.doc", "z23.doc", "z3.doc"];
  39. a.sort(String.naturalCompare);
  40. // ["z1.doc", "z2.doc", "z3.doc", "z10.doc", "z17.doc", "z23.doc"]
  41. // Use wrapper function for case insensitivity
  42. a.sort(function(a, b){
  43. return String.naturalCompare(a.toLowerCase(), b.toLowerCase());
  44. })
  45. // In most cases we want to sort an array of objects
  46. var a = [ {"street":"350 5th Ave", "room":"A-1021"}
  47. , {"street":"350 5th Ave", "room":"A-21046-b"} ];
  48. // sort by street, then by room
  49. a.sort(function(a, b){
  50. return String.naturalCompare(a.street, b.street) || String.naturalCompare(a.room, b.room);
  51. })
  52. // When text transformation is needed (eg toLowerCase()),
  53. // it is best for performance to keep
  54. // transformed key in that object.
  55. // There are no need to do text transformation
  56. // on each comparision when sorting.
  57. var a = [ {"make":"Audi", "model":"A6"}
  58. , {"make":"Kia", "model":"Rio"} ];
  59. // sort by make, then by model
  60. a.map(function(car){
  61. car.sort_key = (car.make + " " + car.model).toLowerCase();
  62. })
  63. a.sort(function(a, b){
  64. return String.naturalCompare(a.sort_key, b.sort_key);
  65. })
  66. ```
  67. - Works well with dates in ISO format eg "Rev 2012-07-26.doc".
  68. ### Custom alphabet
  69. It is possible to configure a custom alphabet
  70. to achieve a desired order.
  71. ```javascript
  72. // Estonian alphabet
  73. String.alphabet = "ABDEFGHIJKLMNOPRSŠZŽTUVÕÄÖÜXYabdefghijklmnoprsšzžtuvõäöüxy"
  74. ["t", "z", "x", "õ"].sort(String.naturalCompare)
  75. // ["z", "t", "õ", "x"]
  76. // Russian alphabet
  77. String.alphabet = "АБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯабвгдеёжзийклмнопрстуфхцчшщъыьэюя"
  78. ["Ё", "А", "Б"].sort(String.naturalCompare)
  79. // ["А", "Б", "Ё"]
  80. ```
  81. External links
  82. --------------
  83. - [GitHub repo][https://github.com/litejs/natural-compare-lite]
  84. - [jsperf test](http://jsperf.com/natural-sort-2/12)
  85. Licence
  86. -------
  87. Copyright (c) 2012-2015 Lauri Rooden &lt;lauri@rooden.ee&gt;
  88. [The MIT License](http://lauri.rooden.ee/mit-license.txt)