How to do string sort by number in specified lines?

==========================================
There is a string(“string_sample”, every line end with “\n”),

name1 bl460ca NoXDisplay (v2012.12) (server1/port1 18709), start Fri

1/24 17:26
name2 shandong NoXDisplay J3_ffffffffa8c0a600_21848_2 (v2012.12)
(server2/port2 13335), start Fri 1/24 9:29
name3 shandong NoXDisplay J3_ffffffffa8c0a600_21848_1 (v2012.12)
(server3/port3 8569), start Fri 1/24 9:26
name4 d58g7m NoXDisplay (v2012.12) (server4/port4 10195), start Wed
1/22 10:30
name5 shandong NoXDisplay J3_ffffffffa8c0a600_21848_3 (v2012.12)
(server5/port5 6975), start Fri 1/24 9:30
name6 bl460cc NoXDisplay (v2012.12) (server6/port6 16114), start Thu
1/23 15:31

==========================================
I want the lines ascending sort by date and time(example:1/22 10:30) as
below.

name4 d58g7m NoXDisplay (v2012.12) (server4/port4 10195), start Wed

1/22 10:30
name6 bl460cc NoXDisplay (v2012.12) (server6/port6 16114), start Thu
1/23 15:31
name3 shandong NoXDisplay J3_ffffffffa8c0a600_21848_1 (v2012.12)
(server3/port3 8569), start Fri 1/24 9:26
name2 shandong NoXDisplay J3_ffffffffa8c0a600_21848_2 (v2012.12)
(server2/port2 13335), start Fri 1/24 9:29
name5 shandong NoXDisplay J3_ffffffffa8c0a600_21848_3 (v2012.12)
(server5/port5 6975), start Fri 1/24 9:30
name1 bl460ca NoXDisplay (v2012.12) (server1/port1 18709), start Fri
1/24 17:26

==========================================
I try write code as below, but don’t work, any friend can give help?
Whether “Schwarzian Transform” better for this?

array_tmp = string_sample.split("\n")
puts array_tmp.sort {|x| x.split(/,/)[1].split(//|\s+|:/)[3]}.sort_by
{|x| x.split(/,/)[1].split(//|\s+|:/)[4]}.sort_by {|x|
x.split(/,/)[1].split(//|\s+|:/)[5]}.sort_by {|x|
x.split(/,/)[1].split(//|\s+|:/)[6]}

On Jan 24, 2014, at 2:23, Previn L. [email protected] wrote:

array_tmp = string_sample.split(“\n”)
puts array_tmp.sort {|x| x.split(/,/)[1].split(//|\s+|:/)[3]}.sort_by
{|x| x.split(/,/)[1].split(//|\s+|:/)[4]}.sort_by {|x|
x.split(/,/)[1].split(//|\s+|:/)[5]}.sort_by {|x|
x.split(/,/)[1].split(//|\s+|:/)[6]}

I can’t really read this blob, so I’m not sure at to it’s point.

You want to “sort by” a subset of the string, numerically, and you
already found “sort_by” (which basically IS the schwartzian transform in
one beautiful method). So why not do just that?

array.lines.sort_by { |s| s.split(",").last.scan(/\d+/).map(&:to_i) 

}

get the lines, sort them by: the last string segment in a comma
separated string, look for digits, and convert them to integers.

I haven’t tested it, and there are probably more elegant ways to do
this, but it should get you moving in the right direction.

Ryan D. wrote in post #1134243:

You want to “sort by” a subset of the string, numerically, and you
already found “sort_by” (which basically IS the schwartzian transform in
one beautiful method). So why not do just that?

array.lines.sort_by { |s| s.split(",").last.scan(/\d+/).map(&:to_i)

}

get the lines, sort them by: the last string segment in a comma
separated string, look for digits, and convert them to integers.

I haven’t tested it, and there are probably more elegant ways to do
this, but it should get you moving in the right direction.

Dear Ryan,

Your solution works fine, I understand now, my method is sort one by
one, at last only last sort take effect. Also my method is too
expatiatory and ugly. :slight_smile:

Many thanks for your guide.

On Fri, Jan 24, 2014 at 3:24 PM, Previn L. [email protected]
wrote:

separated string, look for digits, and convert them to integers.

I haven’t tested it, and there are probably more elegant ways to do
this, but it should get you moving in the right direction.

Your solution works fine, I understand now, my method is sort one by
one, at last only last sort take effect.

I opt for using proper types:

irb(main):035:0> require ‘time’
=> true
irb(main):036:0> puts str.each_line.sort_by {|line|
DateTime.parse(line[/start\s+(.*)/, 1])}
name4 d58g7m NoXDisplay (v2012.12) (server4/port4 10195), start
Wed 1/22 10:30
name6 bl460cc NoXDisplay (v2012.12) (server6/port6 16114), start
Thu 1/23 15:31
name3 shandong NoXDisplay J3_ffffffffa8c0a600_21848_1 (v2012.12)
(server3/port3 8569), start Fri 1/24 9:26
name2 shandong NoXDisplay J3_ffffffffa8c0a600_21848_2 (v2012.12)
(server2/port2 13335), start Fri 1/24 9:29
name5 shandong NoXDisplay J3_ffffffffa8c0a600_21848_3 (v2012.12)
(server5/port5 6975), start Fri 1/24 9:30
name1 bl460ca NoXDisplay (v2012.12) (server1/port1 18709), start
Fri 1/24 17:26
=> nil

Cheers

robert

Previn L. wrote in post #1134339:

May I ask whether there is any forum thread or document I can reference
and study about the usage and grammar of " line[/start\s+(.*)/,
1]) " you used here? How to do if want grab 2 or 3 brackets?

This is one of the (many) uses of String#[]

Notably: str[regexp, capture] → new_str or nil

If a Regexp is supplied, the matching portion of the string is returned.
If a capture follows the regular expression, which may be a capture
group index or name, follows the regular expression that component of
the MatchData is returned instead.

Robert K. wrote in post #1134270:

I opt for using proper types:

irb(main):035:0> require ‘time’
=> true
irb(main):036:0> puts str.each_line.sort_by {|line|
DateTime.parse(line[/start\s+(.*)/, 1])}

Cheers

robert

Wow, really good solution, thank you so much.

May I ask whether there is any forum thread or document I can reference
and study about the usage and grammar of " line[/start\s+(.*)/,
1]) " you used here? How to do if want grab 2 or 3 brackets?

Thanks again.

Matthew K. wrote in post #1134343:

Previn L. wrote in post #1134339:

May I ask whether there is any forum thread or document I can reference
and study about the usage and grammar of " line[/start\s+(.*)/,
1]) " you used here? How to do if want grab 2 or 3 brackets?

This is one of the (many) uses of String#[]

Class: String (Ruby 2.1.0)

Notably: str[regexp, capture] → new_str or nil

If a Regexp is supplied, the matching portion of the string is returned.
If a capture follows the regular expression, which may be a capture
group index or name, follows the regular expression that component of
the MatchData is returned instead.

Dear Mattel,

I understand now, many thanks for your guide.