Skip to content

Instantly share code, notes, and snippets.

@sigwyg
Created February 3, 2020 11:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sigwyg/5532da1cc992dec1d31dca7432513649 to your computer and use it in GitHub Desktop.
Save sigwyg/5532da1cc992dec1d31dca7432513649 to your computer and use it in GitHub Desktop.
指定したIDのアイテムだけソート(未指定のIDの順番は維持する)
/**
* order指定されてないアイテムの順番を維持して、
* 指定アイテムのみソート
*
* @param {array} cards
* @param {array} array of card.id
* @return {array} sorted cards
*/
pointOrder(items, order) {
// console.log('order:', order)
const indexArray = []
const targetItem = []
items.forEach((item, idx) => {
if (order.includes(item.id)) {
indexArray.push(idx)
targetItem.push(item)
}
})
// 任意の順番に並べ替え
targetItem.sort((a, b) => {
return order.indexOf(a.id) - order.indexOf(b.id)
})
// 記録したindexに挿入
indexArray.forEach((pos, idx) => {
items[pos] = targetItem[idx]
})
//console.log( 'sorted:', items.map(item => item.id),)
return items
},
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment