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}
    }, ... ]
3

1 Answer 1

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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.