hi, is there a way in rails that can sort hashes or arrays in a localized alphabetical order? I know we can pass the elements to a block but couldnt figure out how to handle sorting in the block. I would be glad that it can be in an optimized way (comparing 30 letters one by one is byrute force i think) thanks in advance _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
On 11/2/05, Onur Turgay <onurturgay-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> hi, > is there a way in rails that can sort hashes or arrays in a localized > alphabetical order? I know we can pass the elements to a block but couldnt > figure out how to handle sorting in the block. I would be glad that it can > be in an optimized way (comparing 30 letters one by one is byrute force i > think) > > thanks in advancehttp://ruby-doc.org/core/classes/Enumerable.html#M001944 array_of_things.sort array_of_things.sort { |x, y| y <=> x } # reverse order -- rick http://techno-weenie.net
sorting must be in following order (I am not sure if my chars will appear correctly on your readers) a b c ç d e f g h ı i j k l m n o ö p r s ş t u ü v y z On 11/2/05, Rick Olson <technoweenie@gmail.com> wrote:> > On 11/2/05, Onur Turgay <onurturgay@gmail.com> wrote: > > hi, > > is there a way in rails that can sort hashes or arrays in a localized > > alphabetical order? I know we can pass the elements to a block but > couldnt > > figure out how to handle sorting in the block. I would be glad that it > can > > be in an optimized way (comparing 30 letters one by one is byrute force > i > > think) > > > > thanks in advance > > http://ruby-doc.org/core/classes/Enumerable.html#M001944 > > array_of_things.sort > > array_of_things.sort { |x, y| y <=> x } # reverse order > > -- > rick > http://techno-weenie.net > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >_______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Untested, and about as efficient as catching trout with tweezers:
order = %w{a b c ç d e f g h ı i j k l m n o ö p r s ş t u ü v y z}
wordlist = {}
array_of_thing.each{|t|
wordlist[t] = t.split('''').collect{|c|
order.index(c) } }
array_of_thing.sort { |x, y| wordlist[x] <=> wordlist[y] }
Should work with any order you like, but relies on <=>ing arrays, which
I don''t much fancy the speed of.
Hope this helps,
--
Alex
Onur Turgay wrote:> sorting must be in following order (I am not sure if my chars will
> appear correctly on your readers)
>
> a b c ç d e f g h ı i j k l m n o ö p r s ş t u ü v y z
>
> On 11/2/05, * Rick Olson*
<technoweenie-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
> <mailto:technoweenie-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>>
wrote:
>
> On 11/2/05, Onur Turgay
<onurturgay-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
> <mailto:onurturgay-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>>
wrote:
> > hi,
> > is there a way in rails that can sort hashes or arrays in a
localized
> > alphabetical order? I know we can pass the elements to a block but
> couldnt
> > figure out how to handle sorting in the block. I would be glad
> that it can
> > be in an optimized way (comparing 30 letters one by one is byrute
> force i
> > think)
> >
> > thanks in advance
>
> http://ruby-doc.org/core/classes/Enumerable.html#M001944
>
> array_of_things.sort
>
> array_of_things.sort { |x, y| y <=> x } # reverse order
>
> --
> rick
> http://techno-weenie.net
> _______________________________________________
> Rails mailing list
> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
<mailto:Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org>
> http://lists.rubyonrails.org/mailman/listinfo/rails
>
>
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> Rails mailing list
> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
> http://lists.rubyonrails.org/mailman/listinfo/rails