Array#() : () is an Array class method which performs the comparison between the two arrays.
Syntax: Array.()
Parameter: Array for the comparison
Return: -1 : array is less than the other array
0 : array is equal to other array
1 : array is greater than the other array
Example #1 :
a = [18, 22, 33, 4, 5, 6]
b = [5, 4, 22, 1, 88, 9]
c = [18, 22, 33, 40, 50, 6]
puts "comparison : #{a <=> b}\n\n"
puts "comparison : #{a <=> c}\n\n"
puts "comparison : #{b <=> c}\n\n"
|
Output :
comparison : 1
comparison : -1
comparison : -1
Example #2 :
a = ["abc", "xyz", "dog"]
b = ["cow", "cat", "dog"]
c = ["cat", "1", "dog"]
puts "comparison : #{a <=> b}\n\n"
puts "comparison : #{a <=> c}\n\n"
puts "comparison : #{b <=> c}\n\n"
|
Output :
comparison : -1
comparison : -1
comparison : 1
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!