发布于 2024-12-23 14:00:13 字数 318 浏览 2 评论 0原文

我正在使用 Javascript sort (使用 Underscore.js):

_.sortBy(["Bob", "Mary", "Alice"], function (name) {return name})
> ["Alice", "Bob", "Mary"]

我希望数组以另一种方式返回。我该怎么做?

[“玛丽”、“鲍勃”、“爱丽丝”]

我不想在排序后将其反转 - 我希望第一次以相反的方式创建它。

谢谢。

I'm using Javascript sort (with Underscore.js):

_.sortBy(["Bob", "Mary", "Alice"], function (name) {return name})
> ["Alice", "Bob", "Mary"]

I would like the array to return the other way. How do I do that?

["Mary", "Bob", "Alice"]

I don't want to reverse it after it's sorted - I want it to be created the other way around the first time.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

抱着落日 2024-12-30 14:00:13

我宁愿将它与 Array.reverse 充分利用两者的优势。

_.sortBy(["Bob", "Mary", "Alice"], function (name) {return name})
 .reverse()

Instead of throwing underscorejs away, I'd rather use it together with Array.reverse to utilize the best of both.

_.sortBy(["Bob", "Mary", "Alice"], function (name) {return name})
 .reverse()
何以畏孤独 2024-12-30 14:00:13

我只想做 Underscore 在幕后所做的事情:使用 Array#sort< /a> 方法。

["Bob", "Mary", "Alice"].sort(function (a, b) {
    if (a < b) return 1;
    if (b < a) return -1;
    return 0;
});

或者,如果您不想修改原始数组,请先克隆它:

_.clone(["Bob", "Mary", "Alice"]).sort(...)

I would just do what Underscore does under the hood: use the Array#sort method.

["Bob", "Mary", "Alice"].sort(function (a, b) {
    if (a < b) return 1;
    if (b < a) return -1;
    return 0;
});

Or if you don't want the original array modified, clone it first:

_.clone(["Bob", "Mary", "Alice"]).sort(...)
荒路情人 2024-12-30 14:00:13

显然你不应该这样做,因为排序然后反转结果更有意义,但是如果你真的想在排序函数中以相反的顺序排序,你可以这样做......

_.sortBy(["Bob", "Mary", "Alice"], function (a) {
    // split each string into single characters
    // ... then swap for inverse character from unicode range
    // ... and glue the characters back together into an inversely sortable string
    return _.map(a.split(''), function(i){
        return String.fromCharCode(65536 - i.charCodeAt(0));
    }).join('');
});

也值得注意到下划线与本机 JavaScript 排序略有不同,后者在一致排序顺序方面存在一个小的跨平台问题......

如果compareFunction(a, b)返回0,则a和b彼此保持不变,但对所有不同元素进行排序。注意:ECMAscript 标准不保证这种行为,因此并非所有浏览器(例如至少可以追溯到 2003 年的 Mozilla 版本)都尊重这一点。 Array.prototype.sort()< /a>


Underscore 的 .sortBy 文档指出:

返回列表的(稳定)排序副本。 _.sortBy

它不是返回 0 为了保持项目有序,它返回左侧的索引减去右侧的索引。

_.sortBy = function(obj, iteratee, context) {
  iteratee = cb(iteratee, context);
  return _.pluck(_.map(obj, function(value, index, list) {
    return {
      value: value,
      index: index,
      criteria: iteratee(value, index, list)
    };
  }).sort(function(left, right) {
    var a = left.criteria;
    var b = right.criteria;
    if (a !== b) {
      if (a > b || a === void 0) return 1;
      if (a < b || b === void 0) return -1;
    }
    return left.index - right.index;
  }), 'value');
};

Obviously you should not do this, as it makes far more sense to sort, then reverse the results, but if you really want to sort in reverse order inside the sort function, you could do something like this...

_.sortBy(["Bob", "Mary", "Alice"], function (a) {
    // split each string into single characters
    // ... then swap for inverse character from unicode range
    // ... and glue the characters back together into an inversely sortable string
    return _.map(a.split(''), function(i){
        return String.fromCharCode(65536 - i.charCodeAt(0));
    }).join('');
});

... also worth noting that underscore is subtly different than native javascript sort which has a small cross platform issue regarding consistent sort order...

If compareFunction(a, b) returns 0, leave a and b unchanged with respect to each other, but sorted with respect to all different elements. Note: the ECMAscript standard does not guarantee this behaviour, and thus not all browsers (e.g. Mozilla versions dating back to at least 2003) respect this. Array.prototype.sort()

Underscore's .sortBy documentation states:

Returns a (stably) sorted copy of list. _.sortBy

Which it does by instead of returning 0 to keep items in order, it returns the index of the left side minus the index of the right side.

_.sortBy = function(obj, iteratee, context) {
  iteratee = cb(iteratee, context);
  return _.pluck(_.map(obj, function(value, index, list) {
    return {
      value: value,
      index: index,
      criteria: iteratee(value, index, list)
    };
  }).sort(function(left, right) {
    var a = left.criteria;
    var b = right.criteria;
    if (a !== b) {
      if (a > b || a === void 0) return 1;
      if (a < b || b === void 0) return -1;
    }
    return left.index - right.index;
  }), 'value');
};
帥小哥 2024-12-30 14:00:13

您可以在 ES6 中使用 1 行来完成此操作,只需根据您想要的方向更改 > 即可。

.sort() 从 IE6 开始就被支持,你只需传递一个返回 1 或 -1 的函数即可;

["Bob", "Mary", "Alice].sort((a, b) => a > b ? 1 : -1);

You can do this with a 1 liner in ES6, just change the > depending on the direction you want.

.sort() is supported since IE6 and you just pass a function which returns 1 or -1;

["Bob", "Mary", "Alice].sort((a, b) => a > b ? 1 : -1);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文