```On Feb 15, 10:18 am, Erwin <yves_duf... / mac.com> wrote:
> As per Ruby CookBook, I learnt how to sort a simple array by
> frequency :
>
> def sort_by_frequency_descending
>     histogram = inject(Hash.new(0)) { |hash, x| hash[x] += 1; hash}
>     sort_by { |x| [histogram[x] * -1, x]}
> end
> [1,2,3,4,1,2,4,8,1,4,9,16].sort_by_frequency_descending
> # => [1, 1, 1, 4, 4, 4, 2, 2, 3, 8, 9, 16]
>
> Q1:  how should I modify this method, to manipulate more complex
> array,
> like  [ [1, 1, a] ,[2 , 2, b] ,[ 3, 4, c] , [ 4, 5, d] , [1, 2, e] ,
> [ 2, 1, f] , [.....
> where I should sort on the first and second value of each element
>
> this sort_by_frequency_descending is a prelude comparing sucessive
> items of the array, once it is sorted :
>
> b_start = [ [1.0, 1.5, a1] , [1.0, 1.5, a2], [1.0, 1.5, a3], [1.0,
> 1.5, a4], [2.1, 2.5, a5], [2.1, 2.5, a6], [2.1, 2.5, a7], [4.6, 4.2,
> a8], [4.6, 4.2, a9], [4.6, 4.2, a10], [8.3, 8.4, a11], [8.3, 8.4,
> a12], [9.5, 9.3, a13], [16.8, 16.7, a14] ]
>
> when  an item has  first & second values identical to first & second
> values of previous item, then a proc should be applied to both values
> of second item to modify them ( * by random)
> b_start[i+1][0] == b_start[i][0] && _start[i+1][1] == b_start[i][1]
>
> Q2: I am not yet  fluent with this kind of complex process (beyond my
> present readin status of ruby cookbook...)...
>  I'll appreciate any help

For Q1 try:
array_of_arrays.sort_by{|a,b|
if a[0] == b[0]
a[1] <=> b[1]
else
a[0] <=> b[0]
end
}

For Q2, i'll assume you want a new array with the changes (if any):

new_arr_of_arrs = [array_of_arrays[0]] #first item is unchanged as no
previous to compare to
1.upto(array_of_arrays.size) { |i|
p = i-1
if array_of_arrays[p][0] == array_of_arrays[i][0] and
array_of_arrays[p][1] == array_of_arrays[i][1]
new_arr_of_arrs << some_func_retuning
randomized_arr_based_on(array_of_arrays[i])
else
new_arr_of_arrs << array_of_arrays[i]
end
}

I didn't run this code so it may have syntax errors (I think the logic
is good), but hopefully it gets you going

Cheers

```