Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

135
Views
how to order array evenly by property

I have the following array:

arr = [
   { name: 'Apple', store: 'A' },
   { name: 'Banana', store: 'A' },
   { name: 'Carrot', store: 'B' },
   { name: 'Potato', store: 'B' },
   { name: 'Tomato', store: 'A' }
]

I need to sort the array by switching between each store like this:

arr = [
   { name: 'Apple', store: 'A' },
   { name: 'Carrot', store: 'B' },
   { name: 'Banana', store: 'A' },
   { name: 'Tomato', store: 'B' },
   { name: 'Potato', store: 'A' }

]

How can I do that with ruby ?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

I came up with something :)

arr = [
 { name: 'Apple', store: 'A' },
 { name: 'Banana', store: 'A' },
 { name: 'Carrot', store: 'B' },
 { name: 'Potato', store: 'B' },
 { name: 'Tomato', store: 'A' }
]

first, *rest = arr.group_by { |h| h[:store] }.values

first.zip(*rest).flatten.compact 

See it in action: Replit

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!