Closed bylly1 closed 2 years ago
Hello I want to know if is possible to sort array of objects by array of indexes
Here is an example
const data = [ { label: 'Test 2'}, { label: 'Test 1'}, { label: 'Test 3'} ] const order = [2, 1, 3]
Expected result
const data = [ { label: 'Test 1'}, { label: 'Test 2'}, { label: 'Test 3'} ]
You wouldn't want to use this project for that. Some simple code would suffice:
function sortByIndex(data, order) { return order.map(index => data[index]) }
Hello I want to know if is possible to sort array of objects by array of indexes
Here is an example
Expected result