# Sorting numbers

**URL:** <https://www.ruby-forum.com/t/sorting-numbers/183408>\
**Category:** Ruby\
**Created:** [February 13, 2010, 10:20am UTC](https://www.ruby-forum.com/t/sorting-numbers/183408 "2010-02-13T10:20:37Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Andreu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ce73a5/32.png) [@Andreu](https://www.ruby-forum.com/u/Andreu)\
**Post date:** [February 13, 2010, 10:20am UTC](https://www.ruby-forum.com/t/sorting-numbers/183408/1 "2010-02-13T10:20:37Z")

</div>

I read a line from a CSV file as the following:  
fooone,22,114,66,125,footwo  
and I would like to sort the numbers from index  
1 to 4, but I can’t convince the sort function  
to work with integers instead of strings. The result  
of sort is always 114,125,22,66 as it is sorted by  
the first ASCII character.  
The to\_i function doesn’t seem to be appropiate.  
Can the sort work with int’s or is only designed  
to work with strings ?  
I’m using Ruby 1.9.1 p378 with the ‘new’ built-in CSV.

Any clue welcomed, Andreu.

---

<div class="post-metadata">

**Author:** ![Andreu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ce73a5/32.png) [@Andreu](https://www.ruby-forum.com/u/Andreu)\
**Post date:** [February 13, 2010, 11:29am UTC](https://www.ruby-forum.com/t/sorting-numbers/183408/2 "2010-02-13T11:29:40Z")

</div>

On 02/13/2010 10:18 AM, Andreu wrote:

> I’m using Ruby 1.9.1 p378 with the ‘new’ built-in CSV.  
> There are two options:

1. if you want to process the data further, first convert values from  
string to int. Then sorting will work as you expect.

2. if you only want to sort them (e.g. for printing) please have a look  
at #sort\_by.

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![Andreu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ce73a5/32.png) [@Andreu](https://www.ruby-forum.com/u/Andreu)\
**Post date:** [February 13, 2010, 9:11pm UTC](https://www.ruby-forum.com/t/sorting-numbers/183408/3 "2010-02-13T21:11:23Z")

</div>

Thanks Robert and Raul. Both solutions worked.  
I have finally used: row[1…4].sort\_by {|a| a.to\_i}

Regards, Andreu.

---

<div class="post-metadata">

**Author:** ![Andreu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ce73a5/32.png) [@Andreu](https://www.ruby-forum.com/u/Andreu)\
**Post date:** [February 13, 2010, 3:32pm UTC](https://www.ruby-forum.com/t/sorting-numbers/183408/4 "2010-02-13T15:32:21Z")

</div>

Andreu wrote:

> I read a line from a CSV file as the following:  
> fooone,22,114,66,125,footwo  
> and I would like to sort the numbers from index  
> 1 to 4, but I can’t convince the sort function  
> to work with integers instead of strings. The result  
> of sort is always 114,125,22,66 as it is sorted by  
> the first ASCII character.  
> The to\_i function doesn’t seem to be appropiate.  
> Can the sort work with int’s or is only designed  
> to work with strings ?  
> I’m using Ruby 1.9.1 p378 with the ‘new’ built-in CSV.
> 
> Any clue welcomed, Andreu.

Why to you feel .to\_i isn’t appropriate.

[“22”, “114”, “66”, “125”].sort{|a, b| a.to\_i \<=\> b.to\_i}

results in

[“22”, “66”, “114”, “125”]
