Here is a cleaner and shorter version that does not require a bunch of nested if/else cases:
const sorted = arr.sort((a,b) => {
const [a1, a2] = a.title.split(' - ').map(Number);
const [b1, b2] = b.title.split(' - ').map(Number);
return a1 - b1 || a2 - b2;
});
More Related Contents:
- Sorting array of object based on range of Numbers of String [closed]
- Convert string into several integer arrays [duplicate]
- consider a string which has question marks, numbers , letters [closed]
- Sort array of objects by string property value
- How to sort an array of integers correctly
- Javascript Array.sort implementation?
- Sorting an array of objects by property values
- How to sort array inside collection record in MongoDB?
- Sort mixed alpha/numeric array
- Sorting arrays numerically by object property value
- How to sort numbers correctly with Array.sort()? [duplicate]
- How do I sort an array of objects based on the ordering of another array?
- sort Javascript array by two numeric fields
- Sort Array by ISO 8601 date
- How can you sort an array without mutating the original array?
- Sort an array with arrays in it by string
- What is the most efficient way to sort an Html Select’s Options by value, while preserving the currently selected item?
- Javascript – Sort Array of objects by 2 Properties
- Sort array containing objects based on another array [duplicate]
- Does .sort function change original array?
- Remove duplicate objects from an array using javascript
- Populate nested object from array?
- use Lodash to sort array of object by value
- Sorting an Array of JavaScript Objects a Specific Order (using existing function)
- Sort a Javascript Array by frequency and then filter repeats
- sorting object Sunday to Saturday in javascript
- Why doesn’t the sort function of javascript work well?
- How does one sort a multi dimensional array by multiple columns in JavaScript?
- How to sort a JavaScript array of objects by nested object property?
- How to sort an array of objects by date?