Short does not recognize whole date

1

Hello, how do I make short recognize the entire 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 "Data" object 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 the rest does not: /

    
asked by anonymous 08.10.2017 / 09:28

1 answer

4

To organize an array of Date you must use timestamps and not the text version of a date. Use .getTime() and it will work as you want. You will need to convert this Data and Hora to a single Date object before converting 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);
    
08.10.2017 / 09:43