[Ror-es] ordenar un array asociativo por un campo

Miguel Angel Calleja Lÿffffe1zaro mangelcl at yahoo.es
Sat Sep 1 09:51:03 GMT 2007


Hola, 
estoy intendando crearme dos arrays asociativos, unirlos y ordenarlo por el campo fecha, tengo ésto:

        @listado_proyectos=Proyecto.find(:all, :conditions=>["hidden=0 and destacado=1"],:order=>'created_on DESC')
                @arr_proy=[]
                for list in @listado_proyectos
                        @arr_proy << {"id"=>list.id, "fecha"=>list.created_on,"tipo"=>"proyecto"}
                end
        @listado_noticias=Noticia.find(:all, :conditions=>["hidden=0 and destacado=1"],:order=>'created_on DESC')
                @arr_noti=[]
                for list in @listado_noticias
                        @arr_noti << {"id"=>list.id, "fecha"=>list.created_on, "tipo"=>"noticia"}
                end

                @array=@arr_proy.concat(@arr_noti)

mi @array da algo como:
[{"fecha"=>#<Date: 4908687/2,0,2299161>, "id"=>1, "tipo"=>"proyecto"}, {"fecha"=>#<Date: 4908687/2,0,2299161>, "id"=>2, "tipo"=>"proyecto},{"fecha"=>#Date: 4908687/2,0,2299161>, "id"=>4, "tipo"=>"proyecto"}, {"fecha"=>#<Date: 4908687/2,0,2299161>, "id"=>6, "tipo"=>"proyecto"}, {"2299161>, "id"=>7, "tipo"=>"proyecto"}, {"fecha"=>#<Date: 4908687/2,0,2299161>, "id"=>8, "tipo"=>"proyecto"}, {"fecha"=>#<Date: 4, "tipo"=>"proyecto"}, {"fecha"=>#<Date: 4908687/2,0,2299161>, "id"=>10, "tipo"=>"proyecto"}, {"fecha"=>#<Date: 4908687/2,0,22991ia","id"=>1,"tipo"=>"noticia"}]


No se si será ésta la forma correcta de crearlo, he estado viendo la clase Array y Hash, en hash no sabía cómo añadir (<<) asi que probé hacerlo así, ahora me gustaría ordenar mi @array por el campo fecha pero he probado con @array.sort_by {|x| x[1]} y me da este error:
NoMethodError: You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occured while evaluating nil.<=>
        from (irb):32

Alguien me puede decir aparte si lo he hecho bien estos arrays, cómo puedo ordenarlo por el campo fecha? Gracias





       
____________________________________________________________________________________
Sé un Mejor Amante del Cine                         
¿Quieres saber cómo? ¡Deja que otras personas te ayuden!
http://advision.webevents.yahoo.com/reto/entretenimiento.html


More information about the Ror-es mailing list