1
votes

i want to sort in descending order the date value if the name value match with the name property of each element of an array, how can i do?

This is my code:

var name = 'Mario'


array = [
  {name:'Mario',
  date:'/Date(16487438090000)/' },
  {name:'Mario',
  date:'/Date(16887438090000)/' },
  {name: 'Koala',
  date:'/Date(1658743809000)/' },
  {name: 'Jhon',
  date:'/Date(1668743809000)/' },
]

  array.forEach(function(obj){
   if(name === obj.name){
     /*{name:'Mario',date:'/Date(16887438090000)/' }{name:'Mario',date:'/Date(16487438090000)/' },,*/
      //sort date descending
  
   }
 })
And what do you want to do with the elements where the name doesn't match? - Bergi
Are you sure you are getting this kind of date Object?. - Abhishek Kumar Pandey
Is the date string literally of the form '/Date(16487438090000)/'? - jarmod
I would write two functions, one that creates a new array only with the items matching the required name and the other to sort the array. To do the acutal sorting, extract the unixtime stamp from the date-string with substring (constant length of 6 chars before and two chars after the actual value). then create a date object from the timestamp and then compare the two date objects - Michael