How to sort arrays reliably in TypeScript