Meditate on this:
x = [] # => []
input = '1 2 3' # => "1 2 3"
x.push input # => ["1 2 3"]
x.sort # => ["1 2 3"]
Basically, you're entering a single string in your code, pushing it into the array, then sorting a single element array. That's not going to do anything since you can't sort a single element array and get a different order. (Well, you can expect it to be different, but it won't be.)
Instead:
x = [] # => []
x.push '1' # => ["1"]
x.push '3' # => ["1", "3"]
x.push '2' # => ["1", "3", "2"]
That simulates entering three different strings. Sorting x now returns a sorted array:
x.sort # => ["1", "2", "3"]
Here's something else to meditate on:
require 'fruity'
string = ('a'..'z').to_a.join(' ')
string # => "a b c d e f g h i j k l m n o p q r s t u v w x y z"
3.times do
compare do
split1 { string.split }
split2 { string.split(' ') }
split3 { string.split(/\s/) }
split4 { string.split(/\s+/) }
end
end
# >> Running each test 1024 times. Test will take about 1 second.
# >> split1 is faster than split2 by 10.000000000000009% ± 10.0%
# >> split2 is faster than split3 by 3x ± 0.1
# >> split3 is similar to split4
# >> Running each test 2048 times. Test will take about 1 second.
# >> split1 is faster than split2 by 10.000000000000009% ± 10.0%
# >> split2 is faster than split3 by 3x ± 0.1
# >> split3 is similar to split4
# >> Running each test 1024 times. Test will take about 1 second.
# >> split1 is faster than split2 by 10.000000000000009% ± 10.0%
# >> split2 is faster than split3 by 3x ± 0.1
# >> split3 is similar to split4
The results for split1 and split2 are about 10% in favor of split1 so sometimes Fruity reports a 10% difference and sometimes it doesn't. The same is true for split3 and split4.
And:
string = ('a'..'z').to_a.zip(26.times.map{ ' ' * (rand(4) + 1)}).join
string # => "a b c d e f g h i j k l m n o p q r s t u v w x y z "
compare do
split1 { string.split }
split2 { string.split(' ') }
split3 { string.split(/\s/) }
split4 { string.split(/\s+/) }
end
# >> Running each test 1024 times. Test will take about 1 second.
# >> split1 is similar to split2
# >> split2 is faster than split4 by 3x ± 0.1
# >> split4 is faster than split3 by 2.1x ± 0.1 (results differ: ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"] vs ["a", "", "", "", "b", "", "c", "", "", "", "d", "", "", "", "e", "", "f", "", "", "g", "", "h", "", "", "i", "j", "", "k", "l", "", "m", "", "", "", "n", "", "o", "", "p", "q", "r", "", "", "", "s", "", "", "t", "", "", "u", "", "v", "w", "", "x", "", "", "", "y", "z"])
The last result is different because split(/\s/) is only looking for single whitespace characters, not multiples like split(/\s+/) does. I left that result in to show the different the + can make to the engine.