Ruby #sort method doesn’t do well with nil, so clean nil out of your arrays before attempting to sort.

>> arr1 = [nil, "6:1", "1:2", "2:4", "4.3:0.92", "5.0:4", "2.0 to 3"]
#=> [nil, "6:1", "1:2", "2:4", "4.3:0.92", "5.0:4", "2.0 to 3"]
 
>> puts Benchmark.measure { arr1.sort }
#=> ArgumentError: comparison of NilClass with String failed from (irb):53:in `sort' 
 
>> arr1 = ["6:1", "1:2", "2:4", "4.3:0.92", "5.0:4", "2.0 to 3"]
#=> ["6:1", "1:2", "2:4", "4.3:0.92", "5.0:4", "2.0 to 3"]
 
>> puts Benchmark.measure { arr1.sort }
#=>  0.000000   0.000000   0.000000 (  0.000012)

Use a delete(nil) before sorting.

>> arr2 = [nil, "6:1", "1:2", "2:4", "4.3:0.92", "5.0:4", "2.0 to 3"]
#=> [nil, "6:1", "1:2", "2:4", "4.3:0.92", "5.0:4", "2.0 to 3"]
 
>> arr2.delete(nil)
#=> nil
 
>> arr2.sort
#=> ["1:2", "2.0 to 3", "2:4", "4.3:0.92", "5.0:4", "6:1"]

:)

Leave a Reply

Your email address will not be published. Required fields are marked *