gpt4 book ai didi

arrays - Ruby 按每个字符串中的时间值对字符串数组进行排序

转载 作者:太空宇宙 更新时间:2023-11-03 17:43:17 24 4
gpt4 key购买 nike

我有一个这样的字符串数组:

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",
"ANT 105 10 Introduction to Anthropology CMWL 210 TTH 11:30AM-12:45PM Lampman 1/25 1 4.00",
"ANT 107 10 Intro to Envrmntl Archaeology CMWL 101 TTH 11:30AM-12:45PM Markin 2/25 0 4.00",
"ANT 107 90 Intro to Envrmntl Archaeology CMWL 101 TTH 01:00PM-02:15PM Markin 2/15 0 4.00",
"ANT 294 10 SpTp: Queer Anthropology CMWL 210 TTH 01:00PM-02:15PM Neely 0/12 1 4.00",
"ANT 300 10 Language and Culture CMWL 101 TTH 02:30PM-03:45PM Neely 1/18 0 4.00",
"ANT 320 10 Race and Ethnicity CMWL 101 TTH 10:00AM-11:15AM Lampman -4/16 2 4.00",
"ANT 104 10 Intro to World Music & Ethno GCA 204 TTH 10:00AM-11:15AM McCollum, J 0/25 0 4.00",
"ANT 105 10 Introduction to Anthropology CMWL 210 TTH 11:30AM-12:45PM Lampman 1/25 1 4.00",
"ANT 294 10 SpTp: Queer Anthropology CMWL 210 TTH 01:00PM-02:15PM Neely 0/12 1 4.00",
"ANT 300 10 Language and Culture CMWL 101 TTH 02:30PM-03:45PM Neely 1/18 0 4.00",
"ANT 320 10 Race and Ethnicity CMWL 101 TTH 10:00AM-11:15AM Lampman -4/16 2 4.00",
"ANT 104 10 Intro to World Music & Ethno GCA 204 TTH 10:00AM-11:15AM McCollum, J 0/25 0 4.00",
"ANT 105 10 Introduction to Anthropology CMWL 210 TTH 11:30AM-12:45PM Lampman 1/25 1 4.00",
"ANT 300 10 Language and Culture CMWL 101 TTH 02:30PM-03:45PM Neely 1/18 0 4.00",
"ANT 320 10 Race and Ethnicity CMWL 101 TTH 10:00AM-11:15AM Lampman -4/16 2 4.00",
"ANT 104 10 Intro to World Music & Ethno GCA 204 TTH 10:00AM-11:15AM McCollum, J 0/25 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",
"ANT 104 10 Intro to World Music & Ethno GCA 204 TTH 10:00AM-11:15AM McCollum, J 0/25 0 4.00",
"ANT 294 10 SpTp: Queer Anthropology CMWL 210 TTH 01:00PM-02:15PM Neely 0/12 1 4.00",
"AMS 300 10 Toni Morrison DALY 107 TTH 10:00AM-11:15AM Knight 12/20 0 4.00",
]

我想按开始时间对这个数组进行排序,对于 ANT 107 90 Intro to Envrmntl Archaeology,第一个值为 01:00PM。有什么直接的方法可以做到这一点吗?

最佳答案

这是使用 DateTime 的另一种选择:

require 'date'

strings.sort_by! do |item|
time = item.scan(/(\d{2}:\d{2}(PM|AM))/)
DateTime.parse(time.first.first).to_time.to_i
end

puts strings

关于arrays - Ruby 按每个字符串中的时间值对字符串数组进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49393138/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com