排序算法使用块的返回值,在这种情况下,它是比较运算符 ( <=>
) 的结果。何时返回和-1
的顺序将保持不变(即在之前取决于值,算法将保持当前的顺序并在其中知道是否应该在( ) 之前或之后 ( ) 进行排序,或者两者是否是等效()。a
b
a
b
a
b
a
b
-1
1
0
该算法反复比较相邻的元素对,直到所有元素都有序为止。
让我们添加一些输出,看看当您对图书列表调用 sort 时会发生什么。这将使我们对 sort 方法的作用有所了解。我添加了注释来说明排序的每个步骤是如何改变书籍数组的。请注意,在这个例子中,讨论如何排序“交换”位置被简化了。
> books = ["Charlie and the Chocolate Factory", "War and Peace", "Utopia", "A Brief History of Time", "A Wrinkle in Time"]
> books.sort! do |a, b|
> result = a <=> b
> puts %(\n"#{ a }" <=> "#{ b }" #=> #{ result }) # Print out which elements are being compared and the result
> result
> end
"Charlie and the Chocolate Factory" <=> "War and Peace" #=> -1
# ["Charlie and the Chocolate Factory", "War and Peace", "Utopia", "A Brief History of Time", "A Wrinkle in Time"] *** No change
"War and Peace" <=> "Utopia" #=> 1
# ["Charlie and the Chocolate Factory", "Utopia", "War and Peace", "A Brief History of Time", "A Wrinkle in Time"] *** Positions of "Utopia" and "War and Peace" are swapped
"Charlie and the Chocolate Factory" <=> "Utopia" #=> -1
# ["Charlie and the Chocolate Factory", "Utopia", "War and Peace", "A Brief History of Time", "A Wrinkle in Time"] *** No change
"War and Peace" <=> "A Brief History of Time" #=> 1
# ["Charlie and the Chocolate Factory", "Utopia", "A Brief History of Time", "War and Peace", "A Wrinkle in Time"] *** Positions of "War and Peace" and "A Brief History of Time" are swapped
"Utopia" <=> "A Brief History of Time" #=> 1
# ["Charlie and the Chocolate Factory", "A Brief History of Time", "Utopia", "War and Peace", "A Wrinkle in Time"] *** Positions of "Utopia" and "A Brief History of Time" are swapped
"Charlie and the Chocolate Factory" <=> "A Brief History of Time" #=> 1
# ["A Brief History of Time", "Charlie and the Chocolate Factory", "Utopia", "War and Peace", "A Wrinkle in Time"] *** Positions of "Charlie and the Chocolate Factory" and "A Brief History of Time" are swapped
"War and Peace" <=> "A Wrinkle in Time" #=> 1
# ["A Brief History of Time", "Charlie and the Chocolate Factory", "Utopia", "A Wrinkle in Time", "War and Peace"] *** Positions of "War and Peace" and "A Wrinkle in Time" are swapped
"Utopia" <=> "A Wrinkle in Time" #=> 1
# ["A Brief History of Time", "Charlie and the Chocolate Factory", "A Wrinkle in Time", "Utopia", "War and Peace"] *** Positions of "Utopia" and "A Wrinkle in Time" are swapped
"Charlie and the Chocolate Factory" <=> "A Wrinkle in Time" #=> 1
# ["A Brief History of Time", "A Wrinkle in Time", "Charlie and the Chocolate Factory", "Utopia", "War and Peace"] *** Positions of "Charlie and the Chocolate Factory" and "A Wrinkle in Time" are swapped
"A Brief History of Time" <=> "A Wrinkle in Time" #=> -1
# ["A Brief History of Time", "A Wrinkle in Time", "Charlie and the Chocolate Factory", "Utopia", "War and Peace"] *** No change
# Done! All elements have been sorted, so the algorithm exits.
=> ["A Brief History of Time", "A Wrinkle in Time", "Charlie and the Chocolate Factory", "Utopia", "War and Peace"]
当比较从a <=> b
变为 时b <=> a
,结果被反转,这导致排序以相反的顺序进行。
> books = ["Charlie and the Chocolate Factory", "War and Peace", "Utopia", "A Brief History of Time", "A Wrinkle in Time"]
> books.sort! do |a, b|
> result = b <=> a
> puts %(\n"#{ b }" <=> "#{ a }" #=> #{ result }) # Print out which elements are being compared and the result
> result
> end
"A Wrinkle in Time" <=> "A Brief History of Time" #=> 1
"Charlie and the Chocolate Factory" <=> "A Brief History of Time" #=> 1
"Charlie and the Chocolate Factory" <=> "A Wrinkle in Time" #=> 1
"Utopia" <=> "A Brief History of Time" #=> 1
"Utopia" <=> "A Wrinkle in Time" #=> 1
"Utopia" <=> "Charlie and the Chocolate Factory" #=> 1
"War and Peace" <=> "A Brief History of Time" #=> 1
"War and Peace" <=> "A Wrinkle in Time" #=> 1
"War and Peace" <=> "Charlie and the Chocolate Factory" #=> 1
"War and Peace" <=> "Utopia" #=> 1
=> ["War and Peace", "Utopia", "Charlie and the Chocolate Factory", "A Wrinkle in Time", "A Brief History of Time"]
另请参阅Array#sort 的文档!