Sort an array elements based on another array element's class

I am looking for way to sort an Array elements based on another Array element’s class.

Below is such an example->

order = ['String','Integer','NilClass','TrueClass' ]

arry = [1,2,3,4,5, true, false, nil,34,nil, 'Hello', 'World']

How can I sort arry Array values based on order Array element’s class ?

When I sort arry based on order Array, the result should be

result_arry = ['Hello', 'World', 1, 2, 3, 4, 5, 34, nil, nil, true]

Note: In order Array, elements order is dynamic.

Below is answer, got from StackOverflow.

    arry.sort_by{ |v|
        order.map{ |c|
            v.is_a?(Object.const_get(c)) ? -1 : 1
        }
    }
1 Like

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs