# How to do string sort by number in specified lines?

**URL:** <https://www.ruby-forum.com/t/how-to-do-string-sort-by-number-in-specified-lines/238703>\
**Category:** Ruby\
**Created:** [January 24, 2014, 11:23am UTC](https://www.ruby-forum.com/t/how-to-do-string-sort-by-number-in-specified-lines/238703 "2014-01-24T11:23:02Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Detlef\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/58f4c7/32.png) [@Detlef\_R](https://www.ruby-forum.com/u/Detlef_R)\
**Post date:** [January 24, 2014, 11:23am UTC](https://www.ruby-forum.com/t/how-to-do-string-sort-by-number-in-specified-lines/238703/1 "2014-01-24T11:23:02Z")

</div>

# ========================================== 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]}

---

<div class="post-metadata">

**Author:** ![previn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a88e57/32.png) [@previn](https://www.ruby-forum.com/u/previn)\
**Post date:** [January 24, 2014, 12:15pm UTC](https://www.ruby-forum.com/t/how-to-do-string-sort-by-number-in-specified-lines/238703/2 "2014-01-24T12:15:40Z")

</div>

On Jan 24, 2014, at 2:23, Previn L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.

---

<div class="post-metadata">

**Author:** ![previn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a88e57/32.png) [@previn](https://www.ruby-forum.com/u/previn)\
**Post date:** [January 24, 2014, 3:24pm UTC](https://www.ruby-forum.com/t/how-to-do-string-sort-by-number-in-specified-lines/238703/3 "2014-01-24T15:24:49Z")

</div>

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. 🙂

Many thanks for your guide.

---

<div class="post-metadata">

**Author:** ![previn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a88e57/32.png) [@previn](https://www.ruby-forum.com/u/previn)\
**Post date:** [January 24, 2014, 3:38pm UTC](https://www.ruby-forum.com/t/how-to-do-string-sort-by-number-in-specified-lines/238703/4 "2014-01-24T15:38:13Z")

</div>

On Fri, Jan 24, 2014 at 3:24 PM, Previn L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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

---

<div class="post-metadata">

**Author:** ![previn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a88e57/32.png) [@previn](https://www.ruby-forum.com/u/previn)\
**Post date:** [January 25, 2014, 5:37am UTC](https://www.ruby-forum.com/t/how-to-do-string-sort-by-number-in-specified-lines/238703/5 "2014-01-25T05:37:27Z")

</div>

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)](http://ruby-doc.org/core-2.1.0/String.html#method-i-5B-5D)**
>
> Element Reference — If passed a single index, returns a substring of one character at that index. If passed a start index and a length, returns a substring containing length characters starting at the index. If passed a range, its beginning and end...

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.

---

<div class="post-metadata">

**Author:** ![previn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a88e57/32.png) [@previn](https://www.ruby-forum.com/u/previn)\
**Post date:** [January 25, 2014, 4:29am UTC](https://www.ruby-forum.com/t/how-to-do-string-sort-by-number-in-specified-lines/238703/6 "2014-01-25T04:29:43Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![previn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a88e57/32.png) [@previn](https://www.ruby-forum.com/u/previn)\
**Post date:** [January 29, 2014, 2:51am UTC](https://www.ruby-forum.com/t/how-to-do-string-sort-by-number-in-specified-lines/238703/7 "2014-01-29T02:51:42Z")

</div>

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)](http://ruby-doc.org/core-2.1.0/String.html#method-i-5B-5D)
> 
> 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.
