Short does not recognize entire date

Asked

Viewed 33 times

1

Hello, how do I make the short recognize the whole object

{
  "Dir": "Sd:/",
  "List": [
    {
      "Tipo": "P.O.S.-",
      "Data": "11/10/2017",
      "Hora": "23:58",
      "Size": "0",
      "Nome": "Arquivo1"
    },
    {
      "Tipo": "P.-.-.-",
      "Data": "12/05/2017",
      "Hora": "23:57",
      "Size": "0",
      "Nome": "Arquivo2"
    },
    {
      "Tipo": "P.-.-.A",
      "Data": "14/10/2017",
      "Hora": "23:57",
      "Size": "0",
      "Nome": "Arquivo3"
    },
    {
      "Tipo": "P.-.-.-",
      "Data": "16/12/2017",
      "Hora": "23:57",
      "Size": "0",
      "Nome": "Arquivo4"
    },
    {
      "Tipo": "P.-.-.-",
      "Data": "29/09/2017",
      "Hora": "23:57",
      "Size": "0",
      "Nome": "Arquivo5"
    }
  ],
  "NArq": "0",
  "NPast": "5"
}

I want to organize the object "Date" in order of last modification.

var a = a.Data.toLowerCase();
var b = b.Data.toLowerCase();
return a < b ? -1 : a > b ? 1 : 0;

It only detects the first number, but not the rest :/

  • Can you explain better "only detects the first number" and "whole variable"? you’re trying to make .sort() by dates?

  • It does not detect the month, only the first digit. I want to organize a list with dates "08/11/2017" in order of last modification

  • And this a.Data is an object Date or a string in format dd/mm/aaaa/?

  • I’ll edit the question to get better

1 answer

4


To organize an array of Date you have to use timestamps and not the text version of a date. It uses the .getTime() and it will already work as you want. You will have to convert this Data and Hora in an object Date only before making the conversion to timestamp.

Example:

function converterData(data, hora) {
  var partes = data.split('/').reverse().map(Number);
  partes[1]++;
  var hm = hora.split(':').map(Number);

  return new Date(partes[0], partes[1], partes[2], hm[0], hm[1]).getTime();
}

function ordenarDatas(a, b) {
  a = converterData(a.Data, a.Hora);
  b = converterData(b.Data, b.Hora);
  return a < b ? -1 : a > b ? 1 : 0;
}

var desordenado = {
  "Dir": "Sd:/",
  "List": [{
      "Tipo": "P.O.S.-",
      "Data": "29/09/2017",
      "Hora": "23:58",
      "Size": "0",
      "Nome": "Arquivo1"
    },
    {
      "Tipo": "P.-.-.-",
      "Data": "29/09/2037", // <-- 2037!
      "Hora": "23:57",
      "Size": "0",
      "Nome": "Arquivo2"
    },
    {
      "Tipo": "P.-.-.A",
      "Data": "29/09/1997", // <-- 1997
      "Hora": "23:57",
      "Size": "0",
      "Nome": "Arquivo3"
    },
    {
      "Tipo": "P.-.-.-",
      "Data": "29/09/2017",
      "Hora": "23:59",
      "Size": "0",
      "Nome": "Arquivo4"
    },
    {
      "Tipo": "P.-.-.-",
      "Data": "29/09/2017",
      "Hora": "23:57",
      "Size": "0",
      "Nome": "Arquivo5"
    }
  ],
  "NArq": "0",
  "NPast": "5"
}

var ordenado = desordenado.List.sort(ordenarDatas);
console.log(ordenado);

  • 1

    Thank you, solved the problem :)

  • Thanks for the feedback! Votes from users with less than 15 reputation are registered, but do not change the score shown in the post.

  • @Planetwar can sometimes take a while to change the score.

Browser other questions tagged

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