Simplest Answer
array.sort(function(a,b){
// Turn your strings into dates, and then subtract them
// to get a value that is either negative, positive, or zero.
return new Date(b.date) - new Date(a.date);
});
More Generic Answer
array.sort(function(o1,o2){
if (sort_o1_before_o2) return -1;
else if(sort_o1_after_o2) return 1;
else return 0;
});
Or more tersely:
array.sort(function(o1,o2){
return sort_o1_before_o2 ? -1 : sort_o1_after_o2 ? 1 : 0;
});
Generic, Powerful Answer
Define a custom non-enumerable sortBy function using a Schwartzian transform on all arrays :
(function(){
if (typeof Object.defineProperty === 'function'){
try{Object.defineProperty(Array.prototype,'sortBy',{value:sb}); }catch(e){}
}
if (!Array.prototype.sortBy) Array.prototype.sortBy = sb;
function sb(f){
for (var i=this.length;i;){
var o = this[--i];
this[i] = [].concat(f.call(o,o,i),o);
}
this.sort(function(a,b){
for (var i=0,len=a.length;i<len;++i){
if (a[i]!=b[i]) return a[i]<b[i]?-1:1;
}
return 0;
});
for (var i=this.length;i;){
this[--i]=this[i][this[i].length-1];
}
return this;
}
})();
Use it like so:
array.sortBy(function(o){ return o.date });
If your date is not directly comparable, make a comparable date out of it, e.g.
array.sortBy(function(o){ return new Date( o.date ) });
You can also use this to sort by multiple criteria if you return an array of values:
// Sort by date, then score (reversed), then name
array.sortBy(function(o){ return [ o.date, -o.score, o.name ] };
See http://phrogz.net/JS/Array.prototype.sortBy.js for more details.
Answer from Phrogz on Stack OverflowSimplest Answer
array.sort(function(a,b){
// Turn your strings into dates, and then subtract them
// to get a value that is either negative, positive, or zero.
return new Date(b.date) - new Date(a.date);
});
More Generic Answer
array.sort(function(o1,o2){
if (sort_o1_before_o2) return -1;
else if(sort_o1_after_o2) return 1;
else return 0;
});
Or more tersely:
array.sort(function(o1,o2){
return sort_o1_before_o2 ? -1 : sort_o1_after_o2 ? 1 : 0;
});
Generic, Powerful Answer
Define a custom non-enumerable sortBy function using a Schwartzian transform on all arrays :
(function(){
if (typeof Object.defineProperty === 'function'){
try{Object.defineProperty(Array.prototype,'sortBy',{value:sb}); }catch(e){}
}
if (!Array.prototype.sortBy) Array.prototype.sortBy = sb;
function sb(f){
for (var i=this.length;i;){
var o = this[--i];
this[i] = [].concat(f.call(o,o,i),o);
}
this.sort(function(a,b){
for (var i=0,len=a.length;i<len;++i){
if (a[i]!=b[i]) return a[i]<b[i]?-1:1;
}
return 0;
});
for (var i=this.length;i;){
this[--i]=this[i][this[i].length-1];
}
return this;
}
})();
Use it like so:
array.sortBy(function(o){ return o.date });
If your date is not directly comparable, make a comparable date out of it, e.g.
array.sortBy(function(o){ return new Date( o.date ) });
You can also use this to sort by multiple criteria if you return an array of values:
// Sort by date, then score (reversed), then name
array.sortBy(function(o){ return [ o.date, -o.score, o.name ] };
See http://phrogz.net/JS/Array.prototype.sortBy.js for more details.
@Phrogz answers are both great, but here is a great, more concise answer:
array.sort(function(a,b) { return a.getTime() - b.getTime() });
Using the arrow function way
array.sort((a,b) => a.getTime() - b.getTime());
found here: Sort date in Javascript
Try using the Date.getTime() method:
public sortByDueDate(): void {
this.myArray.sort((a: TaskItemVO, b: TaskItemVO) => {
return a.dueDate.getTime() - b.dueDate.getTime();
});
}
^ Above throws error with undefined date so try below:
Edit
If you want to handle undefined:
private getTime(date?: Date) {
return date != null ? date.getTime() : 0;
}
public sortByDueDate(): void {
this.myArray.sort((a: TaskItemVO, b: TaskItemVO) => {
return this.getTime(a.dueDate) - this.getTime(b.dueDate);
});
}
As possible workaround you can use unary + operator here:
public sortByDueDate(): void {
this.myArray.sort((a: TaskItemVO, b: TaskItemVO) => {
return +new Date(a.dueDate) - +new Date(b.dueDate);
});
}
Unary plus (+) converts an operand ( new Date() ) into a number.
Hey community,
I want to write a re-usable function byDate that creates a comparator that I can pass to a sort function like this:
const obj1 = {...somedata, startedAt: new Date()}
...
[obj1, obj2, obj3].sort(byDate('startedAt'))I do have a implementation that looks like this:
export type DateKeys<T> = {
[K in keyof T]: T[K] extends Date | undefined ? K : never
}[keyof T]
export function byDate<T>(d: DateKeys<T>) {
return (a: T, b: T) => a[d].getTime() - b[d].getTime()
}Unfortunately a[d] is types as T[DateKeys<T>] where I hopped it would be Date.
I have the feeling, that there must be a simple solution for this.