kentcdodds / match-sorter

Simple, expected, and deterministic best-match sorting of an array in JavaScript
https://npm.im/match-sorter
MIT License
3.73k stars 129 forks source link

Sort array of objects by array of indexes? #141

Closed bylly1 closed 2 years ago

bylly1 commented 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'}
]
kentcdodds commented 2 years ago

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