我有一个数组
[['Part', 'create'], ['Part', 'update'], ['Part', 'delete'], ['Part', 'release'], ['Plan', 'create'], ['Plan', 'update'], ['Plan', 'delete'], ['Plan', 'release'], ['Inspection', 'create'], ['Inspection', 'update'], ['Inspection', 'delete'], ['Inspection', 'release'], ['User', 'create'],['User', 'update']]
我希望它按零件、计划、检查和创建、更新、删除、发布的顺序排序
我希望它按上述顺序排序的方式是原因
Part
has_many plans
Plan
belongs_to :part
has_many inspections
Inspection
belongs_to :plan
我已经使用 cancan 实现了动态角色和权限。需要为用户分配操作(创建、更新、删除、发布)的权限,并且主题类(零件、计划、检查、用户)的显示顺序必须与其关系相同。
当我使用排序时,我得到
a.sort
=> [["Inspection", "create"], ["Inspection", "delete"], ["Inspection", "release"], ["Inspection", "update"], ["Part", "create"], ["Part", "delete"], ["Part", "release"], ["Part", "update"], ["Plan", "create"], ["Plan", "delete"], ["Plan", "release"], ["Plan", "update"], ["User", "create"], ["User", "update"]]
a.sort_by{|x, y| x[0] <=> y[0]}
=> [["Part", "create"], ["Part", "update"], ["Part", "delete"], ["Part", "release"], ["Plan", "create"], ["Plan", "update"], ["Plan", "delete"], ["Plan", "release"], ["Inspection", "create"], ["Inspection", "update"], ["Inspection", "delete"], ["Inspection", "release"], ["User", "create"], ["User", "update"]]
可以以某种方式使用索引方法吗?以后可能还会添加新的主题类和操作。所以排序顺序可能会改变。