Javascript Lo-Dash sortBy 字符串格式的日期数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26930692/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-22 23:32:41  来源:igfitidea点击:

Lo-Dash sortBy array of dates in string format

javascriptsortinglodash

提问by peresleguine

I'd like to know why lodash doesn't sort array of dates in string format as compared with plain javascript sort(). Is it expected behavior or a bug?

我想知道与普通 javascript 相比,为什么 lodash 不会对字符串格式的日期数组进行排序sort()。这是预期的行为还是错误?

var array = ["2014-11-11", "2014-11-12", null, "2014-11-01", null, null, "2014-11-05"];

_.sortBy(array);
// ["2014-11-11", "2014-11-12", null, "2014-11-01", null, null, "2014-11-05"]

_.sortBy(array, function(value) {return new Date(value);});
// [null, null, null, "2014-11-01", "2014-11-05", "2014-11-11", "2014-11-12"]

array.sort();
// ["2014-11-01", "2014-11-05", "2014-11-11", "2014-11-12", null, null, null]

Version used: Lo-Dash v2.4.1 – Modern build.

使用的版本:Lo-Dash v2.4.1 – 现代版本。

回答by Kiril

If you take a look to lodash code you may see how it's implemented. Function _.sortByinside uses native Array.prototype.sort(see source). But the root is not there. More interesting is function compareAscendingthat is passed as a callback to native sort(source). So in a few words your

如果您查看 lodash 代码,您可能会看到它是如何实现的。_.sortBy内部函数使用 native Array.prototype.sort(见源码)。但根不在那里。更有趣的是compareAscending作为回调传递给 native sort( source) 的函数。所以简而言之,你的

_.sortBy(array, function(value) {return new Date(value);});

_.sortBy(array, function(value) {return new Date(value);});

is converted to:

转换为:

array.sort(function(a, b) {
    var aa = new Date(a),
        bb = new Date(b);

    if (aa !== bb) {
        if (aa > bb) { return 1; }
        if (aa < bb) { return -1; }
    }
    return aa - bb;
})

So why nulls are in the beginning? Because new Date(null)returns Thu Jan 01 1970 01:00:00which is less than any other date in your array.

那么为什么nulls 在一开始呢?因为new Date(null)返回的Thu Jan 01 1970 01:00:00日期小于数组中的任何其他日期。

What about native sort? According to spec (see here) The default sort order is according to string Unicode code points.If simply - native sortconverts items to strings and compares strings. So native sort is smth like:

原生的sort呢?根据规范(参见此处默认排序顺序是根据字符串 Unicode 代码点。如果只是 - 本机sort将项目转换为字符串并比较字符串。所以原生排序就像:

_.sortBy(array, function(value) {return value + ''; });

As soon as 'null' string is always "bigger" than date string (like '2014-11-11') - nulls will be in the tail of the result array.

只要 'null' 字符串始终比日期字符串“大”(如 '2014-11-11') - nulls 将位于结果数组的尾部。

回答by Darsshan

If you are trying to use lodash to sort dates in ascending or descending order for your array of objects, you should use _.orderByinstead of _.sortBy

如果您尝试使用 lodash 为对象数组按升序或降序对日期进行排序,则应使用_.orderBy而不是_.sortBy

https://lodash.com/docs/4.17.15#orderBy, this method allows specifying sort orders either by 'asc'or'desc'

https://lodash.com/docs/4.17.15#orderBy,此方法允许指定排序顺序'asc''desc'

An example would be:

一个例子是:

const sortedArray = _(myArray.orderBy([
  function(object) {
    return new Date(object.date);
  }],["desc"])