Created
February 3, 2020 11:19
-
-
Save sigwyg/5532da1cc992dec1d31dca7432513649 to your computer and use it in GitHub Desktop.
指定したIDのアイテムだけソート(未指定のIDの順番は維持する)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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