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.

34 lines
710 B

4 years ago
  1. var compare = require('./compare');
  2. function mediator(a, b) {
  3. return compare(this, a.converted, b.converted);
  4. }
  5. module.exports = function (array, opts) {
  6. if (!Array.isArray(array) || array.length < 2) {
  7. return array;
  8. }
  9. if (typeof opts !== 'object') {
  10. opts = {};
  11. }
  12. opts.sign = !!opts.sign;
  13. var insensitive = !!opts.insensitive;
  14. var result = Array(array.length);
  15. var i, max, value;
  16. for (i = 0, max = array.length; i < max; i += 1) {
  17. value = String(array[i]);
  18. result[i] = {
  19. value: array[i],
  20. converted: insensitive ? value.toLowerCase() : value
  21. };
  22. }
  23. result.sort(mediator.bind(opts));
  24. for (i = result.length - 1; ~i; i -= 1) {
  25. result[i] = result[i].value;
  26. }
  27. return result;
  28. };