# Array ordering / selection according to elements -

**URL:** <https://www.ruby-forum.com/t/array-ordering-selection-according-to-elements/120080>\
**Category:** Ruby\
**Created:** [November 13, 2007, 4:16pm UTC](https://www.ruby-forum.com/t/array-ordering-selection-according-to-elements/120080 "2007-11-13T16:16:06Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![harper](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b5ac83/32.png) [@harper](https://www.ruby-forum.com/u/harper)\
**Post date:** [November 13, 2007, 4:16pm UTC](https://www.ruby-forum.com/t/array-ordering-selection-according-to-elements/120080/1 "2007-11-13T16:16:06Z")

</div>

given that time1 \< time2 \< time3 \< time4 … (and are all Time objects)

myarray= [time1, time2, time3, time4, time5 , time6 , time7 , …]

special\_sort(time4)= [time3, time4, time5]

…basically, passing an arg of a certain obj in the array to a  
“special\_sort” func, how do i collect the “nearest” ones to it? thx for  
any suggestions

---

<div class="post-metadata">

**Author:** ![harper](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b5ac83/32.png) [@harper](https://www.ruby-forum.com/u/harper)\
**Post date:** [November 13, 2007, 4:39pm UTC](https://www.ruby-forum.com/t/array-ordering-selection-according-to-elements/120080/2 "2007-11-13T16:39:35Z")

</div>

2007/11/13, Shai R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> given that time1 \< time2 \< time3 \< time4 … (and are all Time objects)
> 
> myarray= [time1, time2, time3, time4, time5 , time6 , time7 , …]
> 
> special\_sort(time4)= [time3, time4, time5]
> 
> …basically, passing an arg of a certain obj in the array to a  
> “special\_sort” func, how do i collect the “nearest” ones to it? thx for  
> any suggestions

Define “nearest”. What happens if time4 is not present in the array  
itself? etc.

Cheers

robert

---

<div class="post-metadata">

**Author:** ![harper](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b5ac83/32.png) [@harper](https://www.ruby-forum.com/u/harper)\
**Post date:** [November 13, 2007, 4:46pm UTC](https://www.ruby-forum.com/t/array-ordering-selection-according-to-elements/120080/3 "2007-11-13T16:46:39Z")

</div>

On Nov 13, 9:16 am, Shai R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Posted viahttp://www.ruby-forum.com/.  
> You could do something like this. It only would work for exact  
> matches.

–Dale M.

class Array  
def predecessor(index)  
return nil if index == 0  
self[index-1]  
end  
def successor(index)  
return nil if index \> (self.length-1)  
self[index+1]  
end  
def near(value)  
return nil unless index = self.index(value)  
[self.predecessor(index), self[index], successor(index) ]  
end  
end

a = [1, 2, 3, 4, 6, 7, 8, 9]

puts a.near(0).inspect # not found  
puts a.near(1).inspect  
puts a.near(2).inspect  
puts a.near(3).inspect  
puts a.near(4).inspect  
puts a.near(5).inspect # not found  
puts a.near(6).inspect  
puts a.near(7).inspect  
puts a.near(8).inspect  
puts a.near(9).inspect  
puts a.near(10).inspect # not found

---

<div class="post-metadata">

**Author:** ![harper](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b5ac83/32.png) [@harper](https://www.ruby-forum.com/u/harper)\
**Post date:** [November 13, 2007, 4:56pm UTC](https://www.ruby-forum.com/t/array-ordering-selection-according-to-elements/120080/4 "2007-11-13T16:56:28Z")

</div>

Shai R. wrote:

> given that time1 \< time2 \< time3 \< time4 … (and are all Time objects)
> 
> myarray= [time1, time2, time3, time4, time5 , time6 , time7 , …]
> 
> special\_sort(time4)= [time3, time4, time5]
> 
> …basically, passing an arg of a certain obj in the array to a  
> “special\_sort” func, how do i collect the “nearest” ones to it? thx for  
> any suggestions

min is O(n), gives you only the closest:  
array.min { |a,b| (closest\_to - a).abs \<=\> (closest\_to - b).abs }

sort\_by is O(nlogn), but you can use first(n) to get the n closest  
values:  
array.sort\_by { |e| (closest\_to - e).abs }.first(n)

Regards  
Stefan

---

<div class="post-metadata">

**Author:** ![harper](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b5ac83/32.png) [@harper](https://www.ruby-forum.com/u/harper)\
**Post date:** [November 13, 2007, 4:49pm UTC](https://www.ruby-forum.com/t/array-ordering-selection-according-to-elements/120080/5 "2007-11-13T16:49:34Z")

</div>

Try to include time4 on the array, then sort it. Lood in wich position  
is time4, then get the time before e after it. Isn’t very efficient,  
but works 😃

2007/11/13, Robert K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):
