在Ruby中排序元素的原件指标 [英] originals indexes of sorted elements in Ruby
本文介绍了在Ruby中排序元素的原件指标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
ARR = [1,3,2,4]arr.sort#=> [1,2,3,4]
我想一个数组 [0,2,1,3]
(在 arr.sort
原始指标顺序)
有一个简单的办法做到这一点使用Ruby 1.9.3?
感谢您
解决方案
XS = [1,3,2,4]
original_indexes = xs.each_with_index.sort.map(安培;:上)
#=> [0,2,1,3]
arr = [1,3,2,4]
arr.sort #=> [1,2,3,4]
I would like an array [0, 2, 1, 3]
(original indexes in arr.sort
order)
Is there a simple way to do that with Ruby 1.9.3?
thank you
解决方案
xs = [1, 3, 2, 4]
original_indexes = xs.each_with_index.sort.map(&:last)
#=> [0, 2, 1, 3]
这篇关于在Ruby中排序元素的原件指标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文