3

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

2 Answers 2

5

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));
Sign up to request clarification or add additional context in comments.

Comments

3

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;
}); 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.