Ordering an array of objects by date

Asked

Viewed 8,543 times

10

Well I have an array of objects and I need to sort the one closer to today to further away from today. For example today is day 24/11/2015 there I have in my array the dates:

30/11/2015 
27/11/2015
25/11/2015
30/11/2015 

In case the result I’m needing is that these dates come like this:

25/11/2015
27/11/2015
30/11/2015
30/11/2015

Inside my array I can date, name, and phone.

I tried to use the sort() inside this array however didn’t work. Actually I don’t quite understand why sort()

4 answers

9


I adapted that reply your need, you can create your own function and pass it as function parameter sort.

See Working.

    var objeto = [ 
    { data : new Date('11-30-2015'), nome: 'Marconi', telefone:'32486745425'},
    { data : new Date('11-31-2015'), nome: 'Marcos', telefone:'32486745425'},
    { data : new Date('11-25-2015'), nome: 'B', telefone:'32486745425'},
    { data : new Date('11-27-2015'), nome: 'Testes', telefone:'32486745425'},
];
 			
function compare(a,b) {
  return a.data < b.data;
}

console.log(objeto.sort(compare));

  • your code is ordering from minor to major date, and other, if you add a date with different month, is only considering the day because you are using string :)

  • 1

    @Pedrocamarajunior corrected reply, thanks for the tip.

3

I performed some tests with the function and was performing the ordination wrong, because the date was string. to work properly, you must use a variable of type Date

You can use the sort even, it accepts as optional parameter a function, see an example.

var teste = [
        { nome: "pedro", data: new Date('11-30-2015') },
    	{ nome :"joao", data: new Date('12-01-2015') },
        { nome: "maria", data: new Date('10-05-2015') }
    ];

function ordemDecrescente(a, b) {
    return a.data < b.data;
}

function ordemCrescente(a, b) {
    return a.data > b.data;
}
    
teste.sort(ordemDecrescente);

console.log(teste);

  • I didn’t understand why all answers make use of if (a.data > b.data) return 1; if (a.data < b.data) return -1; return 0; instead of just return a.data > b.data;

  • Because it is @Tobymosque gets much better even. I will change here, thanks for the tip.

1

I used it once:

function date_compare($a, $b)
{
    $t1 = strtotime($a['datetime']);
    $t2 = strtotime($b['datetime']);
    return $t1 - $t2;
}    
usort($array, 'date_compare');

usort - Sorts an array of values using a user-defined comparison function.

The function takes two records from the array at a time and compares, if equals returns zero; if greater, positive number; and if smaller, negative number. The usort uses this result to sort the array.

Taken from the Soen: https://stackoverflow.com/questions/2910611/php-sort-a-multidimensional-array-by-element-containing-date

0

var teste = [{
    nome: "pedro",
    data: new Date('11-30-2018')
  },
  {
    nome: "joao",
    data: new Date('12-01-2018')
  },
  {
    nome: "maria",
    data: new Date('10-05-2018')
  }
];

function ordemDecrescente(a, b) {
  return a.data - b.data;
}

function ordemCrescente(a, b) {
  return b.data - a.data;
}

teste.sort(ordemCrescente);

console.log(teste);

var points = [40, 100, 1, 5, 25, 10];
console.log(points.sort(function(a, b) {
  return b - a
}));

Browser other questions tagged

You are not signed in. Login or sign up in order to post.