Ruby 原地排序数组的一段
PHP中文网
PHP中文网 2017-04-24 09:10:39
0
2
548

ruby 中想对数组中间的一段进行排序,我知道可以这样:

a[1..2] = a[1..2].sort!

但这样会有一次拷贝,出于效率原因希望不产生任何拷贝,直接在原数组上排序。

PHP中文网
PHP中文网

认证0级讲师

reply all(2)
刘奇

Sort only copies the structure of the array, and the contents of the array will not be copied, so the impact on performance is minimal.

What amount of data will make this code a performance bottleneck?

阿神

Using the sort method will regenerate a new array. You can use the array subscript to take out the elements for comparison, and just swap the positions

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!