0

I have a file called highscore.txt which will be read into the program and put into an array. The problem is that I can not sort the array because the points in the .txt file are read as strings, not as integers... how can I fix this?

highscore.txt:

Nico,11\nLuis,7...

Code:

@Bestenliste.each do |stat|
  @sorted = @sorted + [stat.to_s.split(",")]
end
@sorted = @sorted.sort {|a,b| a[1] <=> b[1]}
@sorted.each do |stat2|
  puts stat2[0].to_s + " | " + stat2[1].to_s
end

With stat.to_s.split I split one line (Nico,10) into two objects but the 10 will be a string so I can't sort the array anymore.

And yes I'm doing this in quite a complicated way since I'm new to Ruby. ^^

2
  • 1
    Mind showing the code you tried ? Commented Dec 4, 2014 at 14:06
  • You can get your above code working by just modifying this line @sorted.sort {|a,b| a[1] <=> b[1]} to @sorted.sort {|a,b| a[1].to_i <=> b[1].to_i}. While you can follow @avlazarov's answer as he is correct on that. But if you just want your exiting code to work then you can do the above Commented Dec 4, 2014 at 14:44

3 Answers 3

1

array.sort_by(&:to_i) - will sort the array by calling #to_i for every string, and return an array of the scores still in strings array.map(&:to_i).sort - same but will return an array of ints

Edit: Or use to_f instead of to_i if the scores are floats

Sign up to request clarification or add additional context in comments.

2 Comments

Won't that turn the whole array into integer? I just want the numbers to be converted
Then just array.map(&to_f), where the array holds your string values of the score
0

The simplest way to do this is to convert the convert the strings to integers before comparing them. In the code you posted, this would be as simple as appending a .to_i to the values you are comparing in the line the sorts the array:

@sorted = @sorted.sort {|a,b| a[1].to_i <=> b[1].to_i}

As you seem to have realized though, Ruby is a pretty powerful language. There are much more readable, compact ways of doing what you did. For example, a program which does what you described could easily be implemented in one or two lines:

highscores = File.readlines('highscore.txt').map{|line| line.split(',') }
puts highscores.sort_by{|_, score| score.to_i}.map{|pair| pair.join(" | ")}

Comments

0

You can convert the strings to a different type. Possibly Integer or Float..

For this file:

75.89
87.23
81.98
99.40
45.81

look at this snippet from irb:

1.9.3-p194 :016 > File.readlines('test.txt').each do |line|
1.9.3-p194 :017 >   puts "#{line} - #{line.class}"
1.9.3-p194 :018?>   puts "#{line.to_i} - #{line.to_i.class}"
1.9.3-p194 :019?>   puts "#{line.to_f} - #{line.to_f.class}"
1.9.3-p194 :020?>   end
75.89 - String
75 - Fixnum
75.89 - Float
87.23 - String
87 - Fixnum
87.23 - Float
81.98 - String
81 - Fixnum
81.98 - Float
99.40 - String
99 - Fixnum
99.4 - Float
45.81 - String
45 - Fixnum
45.81 - Float

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.