Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Move x element to the end of an array

Tags:

arrays

ruby

arel

I'm trying to find a method to take a particular relation and move it to the end of the array. Basically, I have a current_account and I want to take this account and move it to the end of the account relationship array so that it will display last when I iteration over the relationships. I want to make a scope and use SQL if possible, here is my attempt and I haven't really gotten anywhere.

HTML

<% current_user.accounts.current_sort(current_account).each do |account| %>
   <li><%= link_to account.name, switch_account_accounts_path(account_id: account.id) %></li>
<% end %>

This current return a list of sorted by created_at accounts. I don't want it to be sorted by created at but the current_account to be at the bottom so I make a scope called current_sort but I'm not sure what to do here.

CURRENT_SORT SCOPE ON ACCOUNT

 scope :current_sort, lambda { |account|

 }

I want this scope to return the passed in account last in the association array. How can I do this with SQL or Ruby?

like image 941
Bitwise Avatar asked Sep 03 '17 18:09

Bitwise


People also ask

How do you move an element to the end of an array?

Initialize two pointers where the left pointer marks the start of the array and the other one that is right one marks the end of the array, respectively. Decrement the count of right pointer long as it points to K, and increment the left pointer as long as it doesn't point to the integer m.

How do you move an element to the end of an array in Python?

Method #1 : Using append() + pop() + index() This particular functionality can be performed in one line by combining these functions. The append function adds the element removed by pop function using the index provided by index function.

How do you move the first element to the end of an array?

When you need to move the first element in an array to the last position, follow this tip. You can use the returned function array. shift() as input to the function array. push().

How do you move an element in an array?

To change the position of an element in an array:Use the splice() method to insert the element at the new index in the array. The splice method changes the original array by removing or replacing existing elements, or adding new elements at a specific index.


2 Answers

A quick trick to sort a particular element to the end of an array is:

array.sort_by { |v| v == current_account ? 1 : 0 }

If you want to move multiple elements it's easier to do:

to_end = [ a, b ]

array - to_end + to_end

Edit: As Stefan points out, this could potentially re-order items. To fix that:

array.sort_by.with_index do |v, i|
  v == current_account ? (array.length + i) : i
end

You can also approach it a different way using partition:

array.partition { |v| v != current_account }.reduce(:+)

Which is a variation on the method used by Stefan in their answer.

like image 81
tadman Avatar answered Oct 23 '22 07:10

tadman


You can use partition to split the array by a condition.

array = [1, 2, 3, 4, 5, 6, 7, 8]
current_account = 3

other_accounts, current_accounts = array.partition { |v| v != current_account }
#=> [[1, 2, 4, 5, 6, 7, 8], [3]]

other_accounts
#=> [1, 2, 4, 5, 6, 7, 8]

current_accounts
#=> [3]

The results can be concatenated:

other_accounts + current_accounts
#=> [1, 2, 4, 5, 6, 7, 8, 3]

or in a single line:

array.partition { |v| v != current_account }.flatten(1)
#=> [1, 2, 4, 5, 6, 7, 8, 3]

# or

array.partition { |v| v != current_account }.inject(:+)
#=> [1, 2, 4, 5, 6, 7, 8, 3]
like image 31
Stefan Avatar answered Oct 23 '22 06:10

Stefan