Reputation: 33
Does anyone know how sort Array with Person by value TaskTime in tasks?
export class Person {
Id: string;
Email: string;
FirstName: string;
LastName: string;
Presence: boolean;
RegisterTime: Date;
tasks: Array<Task>
}
export class Task {
TaskName: string;
DoneTask: boolean;
TaskTime: number;
}
Thanks for help
Upvotes: 3
Views: 9582
Reputation: 2148
Try to follow such way to sort your array. The sample code below will sort the array in descending order by blockId field.
this.itemList.sort((left, right): number => {
if (left.blockId < right.blockId) return 1;
if (left.blockId > right.blockId) return -1;
return 0;
});
Upvotes: 3
Reputation: 164129
The javascript Array
object comes with a builtin sort method, you pass it a compare function like this:
function compare(a, b) {
if (a is less than b by some ordering criterion) {
return -1;
}
if (a is greater than b by the ordering criterion) {
return 1;
}
// a must be equal to b
return 0;
}
So in your case:
let person = new Person();
console.log(person.tasks.sort((task1, task2) => task1.TaskTime - task2.TaskTime));
Upvotes: 5