D
D
Demigodd2018-12-11 17:10:59
JavaScript
Demigodd, 2018-12-11 17:10:59

How to sort objects by date (ascending)?

Link to JSFiddle
How to sort objects by date ( ascending ) only those with var === true .
On the rest, sorting would not pay attention.
For example, through ng-repeat="arr in array | orderBy: 'date'". But this applies to all objects.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
spnq, 2018-12-11
@Demigodd

The easiest way is to make two different arrays and display them separately (if I understand correctly).
Let the original array, as in fiddle

original = [
  {id: 1, name: 'test_1', date: '2018-12-9', var: true},
  {id: 2, name: 'test_2', date: '2018-12-2', var: false},
  {id: 3, name: 'test_3', date: '2018-12-6', var: true},
  {id: 4, name: 'test_4', date: '2018-12-8', var: false},
  {id: 5, name: 'test_5', date: '2018-12-10', var: true},
  ];

Then a new date-sorted array with var === true:
Array with other dates without sorting:
unsortedArray = original.filter( next => !next.var)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question