While sorting /ordering text it considers space first, then ''.'' then characters e.g. @user = User.find(:all,:order=>''name'') it outputs A Sharma A. Bansal ---------------with dot Aarti Dev Is there any way to sort, so that i get the output as Aarti Dev A. Bansal A Sharma -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Jun 10, 2011, at 10:59 AM, Runa wrote:> While sorting /ordering text it considers space first, then ''.'' then > characters > > e.g. @user = User.find(:all,:order=>''name'') > > it outputs > > A Sharma > A. Bansal ---------------with dot > Aarti Dev > > Is there any way to sort, so that i get the output as > Aarti Dev > A. Bansal > A Sharma > >http://www.ruby-doc.org/core/classes/Array.html#M000244 You want to either use the block form or define your own version of <=> to use, stripping punctuation and downcasing the input for good measure to create a "natural" sort. Walter> > > -- > You received this message because you are subscribed to the Google > Groups "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails- > talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org > . > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en > . >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Friday, June 10, 2011 10:13:02 AM UTC-6, Walter Lee Davis wrote:> > > On Jun 10, 2011, at 10:59 AM, Runa wrote: > > > While sorting /ordering text it considers space first, then ''.'' then > > characters > > > > e.g. @user = User.find(:all,:order=>''name'') > > > > it outputs > > > > A Sharma > > A. Bansal ---------------with dot > > Aarti Dev > > > > Is there any way to sort, so that i get the output as > > Aarti Dev > > A. Bansal > > A Sharma > > > > > > http://www.ruby-doc.org/core/classes/Array.html#M000244 > > You want to either use the block form or define your own version of > <=> to use, stripping punctuation and downcasing the input for good > measure to create a "natural" sort. > > Walter >Just beware that if you ever end up using a pagination gem (or rolling your own) such that you get a single "page" of results using limit and offset on your back-end database, this''ll screw up any chance of your custom sorting working correctly. If you''ll never be using LIMIT and OFFSET SQL clauses, then this works great! -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/E0vUsstTT-oJ. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Thanks Walter and Kendall. But ouput which I get using sort mentioned is as follows>> a = [ "A Sharma", "A. Zah", "Aarti Jain", "A. Bansal", "Bindu Lakra" ]=> ["A Sharma", "A. Zah", "Aarti Jain", "A. Bansal", "Bindu Lakra"]>> a.sort {|x,y| y <=> x}=> ["Bindu Lakra", "Aarti Jain", "A. Zah", "A. Bansal", "A Sharma"] I want my sort to skip "." and spaces while sorting. My intended output is => ["Aarti Jain", A. Bansal", "A Sharma","A. Zah", "Bindu Lakra" ] Is there any way to get it sorted the way i wanted where . and space is not considered while sorting? -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 11 June 2011 07:10, Runa <roonadangui-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Thanks Walter and Kendall. > > But ouput which I get using sort mentioned is as follows > >>> a = [ "A Sharma", "A. Zah", "Aarti Jain", "A. Bansal", "Bindu Lakra" ] > => ["A Sharma", "A. Zah", "Aarti Jain", "A. Bansal", "Bindu Lakra"] >>> a.sort {|x,y| y <=> x} > => ["Bindu Lakra", "Aarti Jain", "A. Zah", "A. Bansal", "A Sharma"] > > I want my sort to skip "." and spaces while sorting. > > My intended output is > => ["Aarti Jain", A. Bansal", "A Sharma","A. Zah", "Bindu Lakra" ]You have not shown us the code you have used to do the sort so how can we tell what is wrong with it? Make sure you copy/paste it here rather than re-type it so we can see exactly what you have done. Just the code for the sorting please. First make sure you understand the code you have used. Can you see why it does not do what you want? Colin -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
If I were you I would add a new column PLAIN_NAME or similar and I would strip everything out everything you don''t want in the name and then sort by it. I think it would remove the complexity. You can populate the new column with a before_save callback. On Jun 10, 10:59 am, Runa <roonadan...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> While sorting /ordering text it considers space first, then ''.'' then > characters > > e.g. @user = User.find(:all,:order=>''name'') > > it outputs > > A Sharma > A. Bansal ---------------with dot > Aarti Dev > > Is there any way to sort, so that i get the output as > Aarti Dev > A. Bansal > A Sharma-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.