New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Sort Comparison Function #1493
Comments
Because of the var array = [
moment(),
moment().add(1, 'd'),
moment().subtract(1, 'd')
];
console.log(array.map(function (m) {
return m.format('YYYY-MM-DD')
}));
// ["2014-02-24", "2014-02-25", "2014-02-23"]
array.sort(function (a, b) {
return a - b;
});
console.log(array.map(function (m) {
return m.format('YYYY-MM-DD')
}));
// ["2014-02-23", "2014-02-24", "2014-02-25"] |
Thanks. I was explicitly using |
With typescript, this is not as simple. The typings don't allow you to say Instead, I just say something like:
Not sure if I got that 1 and -1 the right way around. |
You can explicitly call the moment#valueOf as |
I'm partial to: momentsArray.sort((a, b) => a.diff(b)); myself. |
@robertmassaioli be careful with your code,
if a is the same, it would swap them, rather than keep them in place. I know it doesn't sound like much but it could be unexpected for some. |
When sorting a list of moment objects (or objects that have an attribute that is a moment, and should be the sort key), it would be great if there was a comparison function included. So that we could do things like:
The text was updated successfully, but these errors were encountered: