# Array.sort with bignum fails?

**URL:** <https://www.ruby-forum.com/t/array-sort-with-bignum-fails/137947>\
**Category:** Ruby\
**Created:** [May 18, 2008, 8:37pm UTC](https://www.ruby-forum.com/t/array-sort-with-bignum-fails/137947 "2008-05-18T20:37:17Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![rafa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e495f1/32.png) [@rafa](https://www.ruby-forum.com/u/rafa)\
**Post date:** [May 18, 2008, 8:37pm UTC](https://www.ruby-forum.com/t/array-sort-with-bignum-fails/137947/1 "2008-05-18T20:37:17Z")

</div>

I have a array with big numbers and sort method does not order it  
correctly.

irb(main):017:0\> a = [ “8412478247689”, “25977”, “88582”,  
“8410347003107” ]  
=\> [“8412478247689”, “25977”, “88582”, “8410347003107”]  
irb(main):018:0\> a.sort  
=\> [“25977”, “8410347003107”, “8412478247689”, “88582”]  
irb(main):019:0\>

Exists some other form to order the Array?

---

<div class="post-metadata">

**Author:** ![rafa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e495f1/32.png) [@rafa](https://www.ruby-forum.com/u/rafa)\
**Post date:** [May 18, 2008, 8:45pm UTC](https://www.ruby-forum.com/t/array-sort-with-bignum-fails/137947/2 "2008-05-18T20:45:01Z")

</div>

You are using an array of strings, they are sorted alphabetically.

2008/5/18 Rg Rg [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

---

<div class="post-metadata">

**Author:** ![rafa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e495f1/32.png) [@rafa](https://www.ruby-forum.com/u/rafa)\
**Post date:** [May 18, 2008, 8:49pm UTC](https://www.ruby-forum.com/t/array-sort-with-bignum-fails/137947/3 "2008-05-18T20:49:07Z")

</div>

Looks like you are sorting strings not bignums. Therefore the sorting  
is  
correct.

---

<div class="post-metadata">

**Author:** ![rafa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e495f1/32.png) [@rafa](https://www.ruby-forum.com/u/rafa)\
**Post date:** [May 18, 2008, 9:27pm UTC](https://www.ruby-forum.com/t/array-sort-with-bignum-fails/137947/4 "2008-05-18T21:27:14Z")

</div>

Just wasn’t fast enough.

---

<div class="post-metadata">

**Author:** ![rafa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e495f1/32.png) [@rafa](https://www.ruby-forum.com/u/rafa)\
**Post date:** [May 18, 2008, 9:41pm UTC](https://www.ruby-forum.com/t/array-sort-with-bignum-fails/137947/5 "2008-05-18T21:41:42Z")

</div>

Thanks, now this ok.

irb(main):001:0\> a = [8412478247689, 25977, 88582, 8410347003107]  
=\> [8412478247689, 25977, 88582, 8410347003107]  
irb(main):002:0\> a.sort  
=\> [25977, 88582, 8410347003107, 8412478247689]  
irb(main):003:0\>

In my code with “to\_i” it’s ok:

@f.each do |linea|  
cod, desc = linea.chomp.split(/,/)  
@array1[i] = [cod.strip.to\_i]  
i = i+1  
end  
@asort1 = @array1.sort

---

<div class="post-metadata">

**Author:** ![rafa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e495f1/32.png) [@rafa](https://www.ruby-forum.com/u/rafa)\
**Post date:** [May 18, 2008, 9:21pm UTC](https://www.ruby-forum.com/t/array-sort-with-bignum-fails/137947/6 "2008-05-18T21:21:22Z")

</div>

On 18 Mai, 20:37, Rg Rg [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ## Exists some other form to order the Array?
> 
> Posted viahttp://www.ruby-forum.com/.

Try using Integers instead of Strings;

> > a = [8412478247689, 25977, 88582, 8410347003107]  
> > =\> [8412478247689, 25977, 88582, 8410347003107]  
> > a.sort  
> > =\> [25977, 88582, 8410347003107, 8412478247689]

---

<div class="post-metadata">

**Author:** ![rafa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e495f1/32.png) [@rafa](https://www.ruby-forum.com/u/rafa)\
**Post date:** [May 18, 2008, 10:54pm UTC](https://www.ruby-forum.com/t/array-sort-with-bignum-fails/137947/7 "2008-05-18T22:54:23Z")

</div>

Rg Rg wrote:

> @f.each do |linea|  
> Â Â Â cod, desc = linea.chomp.split(/,/)  
> Â Â Â @array1[i] = [cod.strip.to\_i]  
> Â Â Â i = i+1  
> end  
> @asort1 = @array1.sort

An equivalent piece of code that does the same:  
@array1 = @f.map do |linea|  
[linea.to\_i]  
end  
@asort1 = @array1.sort

String#to\_i ignores whitespace at the beginning (so you don’t need  
strip) and  
will stop at the first non digit (so you don’t have to remove what comes  
after the comma - it will just be ignored).

Though I wonder what the purpose of the @array1 variable is and whether  
it is  
intended that @array1 as well as @asort1 only contain one-element  
arrays.  
In case @array1 has no purpose (other than holding the values to be  
sorted  
until they are sorted) and the one-element arrays are not wanted, this  
solution would work:  
@asort1 = @f.map {|line| line.to\_i}.sort

HTH,  
Sebastian

---

<div class="post-metadata">

**Author:** ![rafa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e495f1/32.png) [@rafa](https://www.ruby-forum.com/u/rafa)\
**Post date:** [May 18, 2008, 11:56pm UTC](https://www.ruby-forum.com/t/array-sort-with-bignum-fails/137947/8 "2008-05-18T23:56:56Z")

</div>

Sebastian H. wrote:

> Rg Rg wrote:
> 
> > @f.each do |linea|  
> > cod, desc = linea.chomp.split(/,/)  
> > @array1[i] = [cod.strip.to\_i]  
> > i = i+1  
> > end  
> > @asort1 = @array1.sort
> 
> An equivalent piece of code that does the same:  
> @array1 = @f.map do |linea|  
> [linea.to\_i]  
> end  
> @asort1 = @array1.sort
> 
> String#to\_i ignores whitespace at the beginning (so you don’t need  
> strip) and  
> will stop at the first non digit (so you don’t have to remove what comes  
> after the comma - it will just be ignored).

Ok

> Though I wonder what the purpose of the @array1 variable is and whether  
> it is  
> intended that @array1 as well as @asort1 only contain one-element  
> arrays.  
> In case @array1 has no purpose (other than holding the values to be  
> sorted  
> until they are sorted) and the one-element arrays are not wanted, this  
> solution would work:  
> @asort1 = @f.map {|line| line.to\_i}.sort

I have arrays into @array2

@array2[0] = [25977, “string1”, “string2”]  
@array2[1] = [36000, “string3”, “string4”]  
@array2[ …

My objective is to sort @array2 by numeric code and make a binary  
search.  
Sort it’s ok, and for binary search I use gems --\> “bsearch”.  
“bsearch” with simple array it’s ok, but array into array dont’t work.

This is because I make @array1 with one element.  
Although surely there is some other better form.

> HTH,  
> Sebastian

---

<div class="post-metadata">

**Author:** ![rafa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e495f1/32.png) [@rafa](https://www.ruby-forum.com/u/rafa)\
**Post date:** [May 19, 2008, 12:35am UTC](https://www.ruby-forum.com/t/array-sort-with-bignum-fails/137947/9 "2008-05-19T00:35:50Z")

</div>

On 18 Mai, 23:56, Rg Rg [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > @array1 = @f.map do |linea|
> 
> I have arrays into @array2  
> This is because I make @array1 with one element.  
> Although surely there is some other better form.
> 
> > HTH,  
> > Sebastian
> 
> –  
> Posted viahttp://www.ruby-forum.com/.

there is no “bsearch” gem, at least not in the default repos.  
actually, bsearch seems somewhat outdated (website last modified  
2001-12-23).
