Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

265
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda