kentcdodds/match-sorter

Sort array of objects by array of indexes?

Closed this issue · 1 comments

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])
}