Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a ruby array, and I want to sort all elements starting with index i till index j, in place. The rest of the array should not be modified. How can I implement this?

share|improve this question
    
Partition, sort, join. I don't think there's a readable one-liner. –  CodeGnome 18 mins ago
    
Well-worded question. @CodeGnome, admit it: you've been proved wrong. :-) –  Cary Swoveland 11 mins ago

1 Answer 1

You can use the a[i, j] = a[i, j].sort() to sort from index i to index j. Example:

a = [8, 7, 5, 4, 3]
a[2..4] = a[2..4].sort()
a # => [8, 7, 3, 4, 5]
share|improve this answer
    
I was going to comment it won't do what OP wants, but the change is good –  texasbruce 55 mins ago
    
Very nice, it seems you're on a roll today. –  Cary Swoveland 13 mins ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.