Array.sort with bignum fails?

R

Rg Rg

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?
 
F

Francis Rammeloo

[Note: parts of this message were removed to make it a legal post.]

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

Lee Houghton

Rg said:
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?

Those aren't Bignums, they're Strings, and they're being sorted like
strings (alphabetically).
 
K

konstantin.mailinglists

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?

Try using Integers instead of Strings;
a = [8412478247689, 25977, 88582, 8410347003107] => [8412478247689, 25977, 88582, 8410347003107]
a.sort
=> [25977, 88582, 8410347003107, 8412478247689]
 
R

Rg Rg

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 = [ cod.strip.to_i ]
i = i+1
end
@asort1 = @array1.sort
 
S

Sebastian Hungerecker

Rg said:
@f.each do |linea|
=C2=A0 =C2=A0 =C2=A0 cod, desc =3D linea.chomp.split(/,/)
=C2=A0 =C2=A0 =C2=A0 @array1 =3D [ cod.strip.to_i ]
=C2=A0 =C2=A0 =C2=A0 i =3D i+1
end
@asort1 =3D @array1.sort


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

String#to_i ignores whitespace at the beginning (so you don't need strip) a=
nd=20
will stop at the first non digit (so you don't have to remove what comes=20
after the comma - it will just be ignored).

Though I wonder what the purpose of the @array1 variable is and whether it =
is=20
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=
=20
until they are sorted) and the one-element arrays are not wanted, this=20
solution would work:
@asort1 =3D @f.map {|line| line.to_i}.sort


HTH,
Sebastian
=2D-=20
Jabber: (e-mail address removed)
ICQ: 205544826
 
R

Rg Rg

Sebastian said:
Rg said:
@f.each do |linea|
cod, desc = linea.chomp.split(/,/)
@array1 = [ 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.
 
K

konstantin.mailinglists

Sebastian said:
Rg said:
@f.each do |linea|
      cod, desc = linea.chomp.split(/,/)
      @array1 = [ 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


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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,054
Latest member
LucyCarper

Latest Threads

Top