0

I am trying to order documents by date ( in my case billDate from the example below ) . But i don't want to convert my date into an integer . ( i don't need to ) So i wonder how can i order my string array by date . I would be glad if someone has some examples . Thank you Response structure from the Back end :

[
   {0:
    billAmount: "18.37 EUR"
    billDate: "07.03.2019"
    billStatus: "offen"
    checked: false
    documentCountry: "BG"
    documentId: 100421294
    documentNumber: "2019-BRS-2000000478"
    documentType: "Document 1"
    endDate: "06.03.2019"
    licensePlateNumber: null
    serialNo: null
    startDate: "05.03.2019"
    tableData: {id: 0}
    },
    {1:
    billAmount: "18.37 EUR"
    billDate: "07.03.2019"
    billStatus: "offen"
    checked: false
    documentCountry: "BG"
    documentId: 100421292
    documentNumber: "2019-BRS-2000000478"
    documentType: "Document 2"
    endDate: "06.03.2019"
    licensePlateNumber: null
    serialNo: null
    startDate: "05.03.2019"
    tableData: {id: 1}
    }, ... ]
PandaMastr
  • 687
  • 3
  • 14
  • 35
  • Maybe this answer can help https://stackoverflow.com/a/7513057/3861083 – Fabian Lauer Dec 02 '19 at 08:29
  • Hope this can help https://www.geeksforgeeks.org/sort-an-array-of-string-of-dates-in-ascending-order/ | https://flaviocopes.com/how-to-sort-array-by-date-javascript/ – Hardik Chaudhary Dec 02 '19 at 08:29
  • Does this answer your question? [Sort a string date array](https://stackoverflow.com/questions/30691066/sort-a-string-date-array) – Easwar Dec 02 '19 at 08:30

1 Answers1

1
// Assuming the array is in variable response

function compare(a, b) {
  const date1 = a.billDate.split('.').reverse().join('');
  const date2 = b.billDate.split('.').reverse().join('');
  return date1.localeCompare(date2);    
}

const sortedResponse = response.sort(compare);
Easwar
  • 5,132
  • 1
  • 11
  • 21