0

I have an array of objects with properties like name, lastUpdated, etc. The objects are listed in a grid that has sorting options for each of these properties. I have an option also to add a new object to the list. What I want to do is to add it ordered on the list following the sorting criteria applied on that moment.

For example if I have this list:

aaaa
bbbb
hhhh

with a sorting criteria of name ASC and I add a name like cccc I want my list like this:

aaaa
bbbb
cccc
hhhh

And the same with any sorting criteria applied.

When I apply a sorting criteria I save it on my component on a variable:

sortingCriteria;

Then when I add a new element to the list I do like:

this.list.sort((a, b) => a.Name.localeCompare(b.Name));

That orders by name but I need to apply that sortingCriteria saved before on this sorting method to order the list properly. Is there any way to use it as a property of the list's objects? Or something to do to filter my list with that sortingCriteria saved? I've tried to use my sorting method which is this one:

sortGrid(event) {
    this.sortingCriteria = event.field;
    if (this.list&& this.cols && this.list.length > 0 && this.cols.length > 0) {
      const col = this.cols.filter(x  => x.field === event.field)[0];

      if (col) {
        this.colIndexSelected = col.colIndex;
      }
    }
  }

But It's not working I don't know why.

pedrodotnet
  • 788
  • 3
  • 16
  • 34
  • let me check that post – pedrodotnet Jul 30 '19 at 13:46
  • That uses javascript I've tried that solution but it's not working on this case – pedrodotnet Jul 30 '19 at 14:04
  • 1
    "That uses javascript"... What do you think Angular is written in? :) I assure you, at least one of the answers to that question will work to sort an array of objects based on a property. Adapting the code to your situation might take some thought; you may not be able to just copy and paste the code directly into your method and get a result, but that's part of programming. – Heretic Monkey Jul 30 '19 at 14:59

0 Answers0