i have an array
[['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']]
i want it to be sorted in order of Part, Plan, Inspection and create, update, delete, release
the way i want it to be sorted in the above order is cause
Part
has_many plans
Plan
belongs_to :part
has_many inspections
Inspection
belongs_to :plan
i have implemented dynamic roles and permissions using cancan. the user needs to be assigned permissions for the actions(create, update, delete, release) and the order of display of the
SubjectClass(Part, Plan, Inspection, User) must be as their relationship.
when i used sort, i get
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"]]
can index method be used in someway? Also new subjectclasses and actions maybe added later. So sorting order may change.
If we assume that all unspecified elements have weight of 999 (such as
Userin your example), this will sort the input array in a given order: