我是Ruby新手,希望仅对我的集合中的某些项目进行排序。 例如,如果我有以下数组。我只想包含该属性type: 'sort'
对数组中的特定对象进行排序
object = [{
type: 'sort',
id: 3
}, {
type: 'notsort',
id: 4
}, {
type: 'sort',
id: 1
}, {
type: 'sort',
id: 0
}
]
我需要为了直接映射到下面的ID映射的对象进行排序。
sortIdOrder = [0, 1, 3]
最终的结果应该是这样的:
object = [{
type: 'notsort',
id: 4
}, {
type: 'sort',
id: 0
},{
type: 'sort',
id: 1
}, {
type: 'sort',
id: 3
}]
正如你可以看到阵列由id
基础上,sortIdOrder
排序。 notsort
type
可以在最后或开始。
作为说明,约定认为Ruby方法和变量应该是'sort_id_order'形式。 – tadman