Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8669935
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T18:35:45+00:00 2026-06-12T18:35:45+00:00

i have an array [[‘Part’, ‘create’], [‘Part’, ‘update’], [‘Part’, ‘delete’], [‘Part’, ‘release’], [‘Plan’, ‘create’],

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T18:35:46+00:00Added an answer on June 12, 2026 at 6:35 pm
    a = [['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']]
    order = [%w{Part Plan Inspection}, %w{create update delete release}]
    

    If we assume that all unspecified elements have weight of 999 (such as User in your example), this will sort the input array in a given order:

    a.sort_by{|e| e.zip(order).map{|i,j| j.index(i) || 999}}
    => [["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"]]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have array with colors as its objects. I want to create an image
I have array object in jquery. array=[]; array{Id:user.Id,Date:user.Date}; Now am going to pass this
i have array like below which is sorted by array_count_values function, Array ( [Session
I have array of class Person in ViewModel and I want to show their
I have array of words and I want to get a hash, where keys
I have array that element is hash a = [{:history_date=>15/07/10}, {:open_price=>7.90}] I want to
I have array data as shown below, I want to store 'sale' value into
I have array of objects with created images (Object.Image), i want to show images
I have array of objects. I want to write method wich i will use
I have array of country names and i want to get coordinates of them

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.