Ruby在每个字符串中按时间值排序字符串数组

时间:2018-03-20 20:01:24

标签: arrays ruby string date time

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

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",
]

我想按开始时间对此数组进行排序,01:00PM的第一个值为ANT 107 90 Intro to Envrmntl Archaeology。有没有直接的方法呢?

4 个答案:

答案 0 :(得分:3)

这是使用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

答案 1 :(得分:1)

这是一种基于您的数据的快速入侵方法。并不是真正按时间排序(忽略AM / PM)只是按时间作为数值。

strings.sort! { |x,y|
  # split on time delimeter
  s = x.index('-')
  # sort by time as numerical
  x[s-7..s-1] <=> y[s-7..s-1]
}

puts strings

输出:

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
AMS 210 11  Intro to American Lit II        SMTH 222  TTH    01:00PM-02:15PM  DeProspo,R   1/25        0      4.00
ANT 294 10  SpTp: Queer Anthropology        CMWL 210  TTH    01:00PM-02:15PM  Neely        0/12        1      4.00
ANT 294 10  SpTp: Queer Anthropology        CMWL 210  TTH    01:00PM-02:15PM  Neely        0/12        1      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
ANT 300 10  Language and Culture            CMWL 101  TTH    02:30PM-03:45PM  Neely        1/18        0      4.00
AMS 394 11  SpTp: Public Opinion Amer Pol   DALY 107  TTH    02:30PM-03:45PM  Cossette     5/16        0      4.00
...
...

答案 2 :(得分:0)

您可以尝试:

data = <<data.split("\n")
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:15A
data

puts data.sort_by{|x| 
  x =~ /\s(\d\d:\d\d)([AP])M/
  '%s%s' % [$2=='A' ? 0 : 1,$1]
  }

您的数据不需要$2=='A' ? 0 : 1,但它与

等数据相关
data = <<data.split("\n")
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    02:00AM-12:45PM  DeProspo,R   0/25        0      4.00
data

答案 3 :(得分:0)

arr = [
 "ANT 107 90  Intro to Archaeology   01:00PM-02:15PM  Markin     ",
 "AMS 210 10  Intro to Lit I         11:30AM-12:45PM  DeProspo,R ",
 "AMS 210 11  Intro to Lit II        02:00PM-03:15PM  DeProspo,R ",
 "AMS 300 10  Toni Morrison          10:00AM-11:15AM  Knight     "
]

arr.sort_by { |s| [s[40], s[35,5]] }
  #=> ["AMS 300 10  Toni Morrison          10:00AM-11:15AM  Knight     ",
  #    "AMS 210 10  Intro to Lit I         11:30AM-12:45PM  DeProspo,R ",
  #    "ANT 107 90  Intro to Archaeology   01:00PM-02:15PM  Markin     ",
  #    "AMS 210 11  Intro to Lit II        02:00PM-03:15PM  DeProspo,R "]

请注意,如果s = arr.first

[s[40], s[35,5]]
  #=> ["P", "01:00"]

Enumerable#sort_by文档的第三段解释了Array#<=>使用的数组顺序。字符串的排序(例如,比较"P""A""01:00""01:20")在String#<=>的文档中进行了解释。["A", "11:30"]["P", "01:00"]之前排序,因为"A""P"之前。同样,["P", "01:00"]["P", "02:00"]之前排序,因为两个数组的第一个元素相等,而"01:00""02:00"之前。

也可以写

arr.sort_by { |s| s[40] + s[35,5] }

s = arr.first然后

s[40] + s[35,5]
  #=> "P01:00"