Class: Array

Inherits:
Object show all
Defined in:
lib/RubyExt/array.rb

Instance Method Summary collapse

Instance Method Details

#sort_by_weight(weight) ⇒ Object



2
3
4
# File 'lib/RubyExt/array.rb', line 2

def sort_by_weight weight
  clone.sort_by_weight! weight.clone   
end

#sort_by_weight!(weight) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/RubyExt/array.rb', line 6

def sort_by_weight! weight
  size.times do |i|
    iteration = size - i - 1
    break if iteration < 0
    iteration.times do |j|
      if weight[j] > weight[j+1]
        buf = self[j]
        self[j] = self[j+1]
        self[j+1] = buf
        
        buf = weight[j]
        weight[j] = weight[j+1]
        weight[j+1] = buf
      end
    end
  end
  return self
end