You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Ruby按字符串中的时间值对字符串数组进行排序

Sorting Course Strings by Time in Ruby

Great question! To sort your array of course strings by their start time, we’ll break this into three straightforward steps: extract the start time from each string, convert it to a comparable value, and then sort the array using that value. Here’s a complete implementation:

Step 1: Helper Function to Convert Time to Minutes

First, we need a way to convert AM/PM time strings into a numerical value (total minutes since midnight) that’s easy to compare. This avoids dealing with Time objects and ensures consistent sorting:

def time_to_minutes(time_str)
  # Split into hour, minute, and period (AM/PM)
  hour, minute_part = time_str.split(':')
  minute, period = minute_part.split(/(AM|PM)/)
  
  hour = hour.to_i
  minute = minute.to_i

  # Adjust hour for AM/PM edge cases
  if period == 'PM' && hour != 12
    hour += 12
  elsif period == 'AM' && hour == 12
    hour = 0
  end

  # Calculate total minutes since midnight
  hour * 60 + minute
end

Step 2: Extract Start Time and Sort the Array

Next, we use Ruby’s sort_by method to sort the array. For each course string, we:

  1. Use a regex to find the start time (before the hyphen in the time range)
  2. Convert that time to minutes using our helper function
  3. Sort the array based on these minute values
strings = [ 
  "ANT 107 90 Intro to Envrmntl Archaeology CMWL 101 TTH 01:00PM-02:15PM Markin 2/15 0 4.00", 
  "AMS 210 10 Intro to American Lit II SMTH 222 TTH 11:30AM-12:45PM DeProspo,R 0/25 0 4.00", 
  "AMS 210 11 Intro to American Lit II SMTH 222 TTH 01:00PM-02:15PM DeProspo,R 1/25 0 4.00", 
  "AMS 300 10 Toni Morrison DALY 107 TTH 10:00AM-11:15AM Knight 12/20 0 4.00", 
  "AMS 394 11 SpTp: Public Opinion Amer Pol DALY 107 TTH 02:30PM-03:45PM Cossette 5/16 0 4.00"
]

sorted_courses = strings.sort_by do |course|
  # Extract the start time using regex
  start_time_match = course.match(/(\d{2}:\d{2}(AM|PM))-/)
  start_time = start_time_match[1]
  
  # Convert to minutes for sorting
  time_to_minutes(start_time)
end

# Print the sorted result
sorted_courses.each { |course| puts course }

Expected Output

When you run this code, the sorted array will be ordered by start time from earliest to latest:

AMS 300 10 Toni Morrison DALY 107 TTH 10:00AM-11:15AM Knight 12/20 0 4.00
AMS 210 10 Intro to American Lit II SMTH 222 TTH 11:30AM-12:45PM DeProspo,R 0/25 0 4.00
ANT 107 90 Intro to Envrmntl Archaeology CMWL 101 TTH 01:00PM-02:15PM Markin 2/15 0 4.00
AMS 210 11 Intro to American Lit II SMTH 222 TTH 01:00PM-02:15PM DeProspo,R 1/25 0 4.00
AMS 394 11 SpTp: Public Opinion Amer Pol DALY 107 TTH 02:30PM-03:45PM Cossette 5/16 0 4.00

Key Notes

  • If two courses start at the same time, they’ll retain their original order in the array (since sort_by is stable in Ruby 2.1+).
  • The regex (\d{2}:\d{2}(AM|PM))- reliably captures the start time by looking for a time pattern followed by a hyphen, which works perfectly for your given string format.
  • The helper function handles edge cases like 12AM (converted to 0 minutes) and 12PM (converted to 720 minutes) correctly.

内容的提问来源于stack exchange,提问作者Lyres

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 12:12:46