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

263
Views
How to sort an an array having mulitple time slots

I have an array collected_slots = ["09:30", "09:45", "09:40", "09:55", "09:35", "09:50"]

I have to sort it on the bases of the time, I have tried

sorted_slots = collected_slots.sort_by do |slot|
parsed = collected_slots[:time].split('-').first
[parsed&.to_i, parsed.split(':').second&.to_i]

but it doesn't work

What I want is collected_slots = ["09:30-09:35", "09:35-09:40", "09:40-09:45", "09:45-09:50", "09:50-09:55"]

I have tried but it doesn't works. Please help me with this.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

I think this should do what you want.

  1. Sort the entries after converting each entry to a DateTime object.
  2. map the index, except the last index, of each entry to the value at that index and the value at the next index
require 'date'
input = ["09:30", "09:45", "09:40", "09:55", "09:35", "09:50"]
sorted = input.sort_by {DateTime.parse(_1)}
output = sorted[0...-1].each_index.map {"#{sorted[_1]}-#{sorted[_1+1]}"}
=> ["09:30-09:35", "09:35-09:40", "09:40-09:45", "09:45-09:50", "09:50-09:55"] 
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!