<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	>
<channel>
	<title>Comments on: Ruby: Sort an array of objects by an attribute</title>
	<atom:link href="http://ariejan.net/2007/01/28/ruby-sort-an-array-of-objects-by-an-attribute/feed/" rel="self" type="application/rss+xml" />
	<link>http://ariejan.net/2007/01/28/ruby-sort-an-array-of-objects-by-an-attribute/</link>
	<description>May The Source Be With You.</description>
	<pubDate>Tue, 06 Jan 2009 09:07:10 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.7</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Eduardo</title>
		<link>http://ariejan.net/2007/01/28/ruby-sort-an-array-of-objects-by-an-attribute/comment-page-1/#comment-10639</link>
		<dc:creator>Eduardo</dc:creator>
		<pubDate>Mon, 24 Nov 2008 11:48:10 +0000</pubDate>
		<guid isPermaLink="false">http://beta.ariejan.net/?p=77#comment-10639</guid>
		<description>If someone want to know, i solved my stuff. Just did a sort_by{&#124;x&#124;[A,B...]}

Where A,B and so on can be any attribute, even if a result of a relation just like i wish: sort_by {&#124;x&#124;[x.rating,x.name,-x.people.size]}

And you can use minus signal before any therm to mean DESC order cuz the default is ASC order.</description>
		<content:encoded><![CDATA[<p>If someone want to know, i solved my stuff. Just did a sort_by{|x|[A,B...]}</p>
<p>Where A,B and so on can be any attribute, even if a result of a relation just like i wish: sort_by {|x|[x.rating,x.name,-x.people.size]}</p>
<p>And you can use minus signal before any therm to mean DESC order cuz the default is ASC order.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Eduardo</title>
		<link>http://ariejan.net/2007/01/28/ruby-sort-an-array-of-objects-by-an-attribute/comment-page-1/#comment-10625</link>
		<dc:creator>Eduardo</dc:creator>
		<pubDate>Fri, 31 Oct 2008 19:07:54 +0000</pubDate>
		<guid isPermaLink="false">http://beta.ariejan.net/?p=77#comment-10625</guid>
		<description>Im trying to sort with two atributtes but the first one is "name" and comes from model "Place" and the second comes from the relation of the Place with another model called "Rattings". The "Rattings" gives to "Place" an atributte "rating" when i use "place.rating"

How can i sort the Places first by rating and that result I sort by name without mess the rating order ? Its a stable sorting proccess and i dont know how to do.</description>
		<content:encoded><![CDATA[<p>Im trying to sort with two atributtes but the first one is &#8220;name&#8221; and comes from model &#8220;Place&#8221; and the second comes from the relation of the Place with another model called &#8220;Rattings&#8221;. The &#8220;Rattings&#8221; gives to &#8220;Place&#8221; an atributte &#8220;rating&#8221; when i use &#8220;place.rating&#8221;</p>
<p>How can i sort the Places first by rating and that result I sort by name without mess the rating order ? Its a stable sorting proccess and i dont know how to do.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: bmac</title>
		<link>http://ariejan.net/2007/01/28/ruby-sort-an-array-of-objects-by-an-attribute/comment-page-1/#comment-10622</link>
		<dc:creator>bmac</dc:creator>
		<pubDate>Mon, 27 Oct 2008 17:06:29 +0000</pubDate>
		<guid isPermaLink="false">http://beta.ariejan.net/?p=77#comment-10622</guid>
		<description>Hi,
what if I have an array of dates in this format?
["", "2008/7/27", "2008/10/27", "2008/10/24"]
or this
["", "2008 7 27", "2008 10 27", "2008 10 24"]

thanks</description>
		<content:encoded><![CDATA[<p>Hi,<br />
what if I have an array of dates in this format?<br />
["", "2008/7/27", "2008/10/27", "2008/10/24"]<br />
or this<br />
["", "2008 7 27", "2008 10 27", "2008 10 24"]</p>
<p>thanks</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: william</title>
		<link>http://ariejan.net/2007/01/28/ruby-sort-an-array-of-objects-by-an-attribute/comment-page-1/#comment-10594</link>
		<dc:creator>william</dc:creator>
		<pubDate>Sun, 31 Aug 2008 01:02:43 +0000</pubDate>
		<guid isPermaLink="false">http://beta.ariejan.net/?p=77#comment-10594</guid>
		<description>excelente, me sirvio mucho!!!!!!!!!!</description>
		<content:encoded><![CDATA[<p>excelente, me sirvio mucho!!!!!!!!!!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Kristina Lim</title>
		<link>http://ariejan.net/2007/01/28/ruby-sort-an-array-of-objects-by-an-attribute/comment-page-1/#comment-10537</link>
		<dc:creator>Kristina Lim</dc:creator>
		<pubDate>Wed, 04 Jun 2008 06:27:18 +0000</pubDate>
		<guid isPermaLink="false">http://beta.ariejan.net/?p=77#comment-10537</guid>
		<description>(Oops. Character codes.)

Regarding Adam's question on sorting by more than one attribute, a sort block that returns:

&lt;code&gt;[a.attrib_a, a.attrib_b] &#60;=&#62; [b.attrib_a, b.attrib_b]&lt;/code&gt;

for a and b should sort an array of objects first by attrib_a, and then by attrib_b. For more elaborate sorting, I sometimes have to put in expressions in the arrays.

Array &#60;=&#62;(): http://www.ruby-doc.org/core/classes/Array.html#M000316</description>
		<content:encoded><![CDATA[<p>(Oops. Character codes.)</p>
<p>Regarding Adam&#8217;s question on sorting by more than one attribute, a sort block that returns:</p>
<p><code>[a.attrib_a, a.attrib_b] &lt;=&gt; [b.attrib_a, b.attrib_b]</code></p>
<p>for a and b should sort an array of objects first by attrib_a, and then by attrib_b. For more elaborate sorting, I sometimes have to put in expressions in the arrays.</p>
<p>Array &lt;=&gt;(): <a href="http://www.ruby-doc.org/core/classes/Array.html#M000316" rel="nofollow" onclick="javascript:urchinTracker ('/outbound/comment/www.ruby-doc.org');">http://www.ruby-doc.org/core/classes/Array.html#M000316</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Kristina Lim</title>
		<link>http://ariejan.net/2007/01/28/ruby-sort-an-array-of-objects-by-an-attribute/comment-page-1/#comment-10536</link>
		<dc:creator>Kristina Lim</dc:creator>
		<pubDate>Wed, 04 Jun 2008 06:19:31 +0000</pubDate>
		<guid isPermaLink="false">http://beta.ariejan.net/?p=77#comment-10536</guid>
		<description>Regarding Adam's question on sorting by more than one attribute, a sort block that returns:

&lt;code&gt;[a.attrib_a, a.attrib_b]  [b.attrib_a, b.attrib_b]&lt;/code&gt;

for a and b should sort an array of objects first by attrib_a, and then by attrib_b. For more elaborate sorting, I sometimes have to put in expressions in the arrays.

Array (): http://www.ruby-doc.org/core/classes/Array.html#M000316</description>
		<content:encoded><![CDATA[<p>Regarding Adam&#8217;s question on sorting by more than one attribute, a sort block that returns:</p>
<p><code>[a.attrib_a, a.attrib_b]  [b.attrib_a, b.attrib_b]</code></p>
<p>for a and b should sort an array of objects first by attrib_a, and then by attrib_b. For more elaborate sorting, I sometimes have to put in expressions in the arrays.</p>
<p>Array (): <a href="http://www.ruby-doc.org/core/classes/Array.html#M000316" rel="nofollow" onclick="javascript:urchinTracker ('/outbound/comment/www.ruby-doc.org');">http://www.ruby-doc.org/core/classes/Array.html#M000316</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Richard Forster</title>
		<link>http://ariejan.net/2007/01/28/ruby-sort-an-array-of-objects-by-an-attribute/comment-page-1/#comment-4919</link>
		<dc:creator>Richard Forster</dc:creator>
		<pubDate>Fri, 04 Jan 2008 05:12:12 +0000</pubDate>
		<guid isPermaLink="false">http://beta.ariejan.net/?p=77#comment-4919</guid>
		<description>In the previous post the code got a little mangled: try again with simplification
&lt;code&gt;
teams_members.sort! do &#124;a,b&#124; 
      a.team == nil ? akey = a.name : akey = a.name +  a.team
      b.team == nil ? bkey = b.name : bkey = b.name +  b.team
      akey  bkey       
    end
&lt;/code&gt;</description>
		<content:encoded><![CDATA[<p>In the previous post the code got a little mangled: try again with simplification<br />
<code><br />
teams_members.sort! do |a,b|<br />
      a.team == nil ? akey = a.name : akey = a.name +  a.team<br />
      b.team == nil ? bkey = b.name : bkey = b.name +  b.team<br />
      akey  bkey<br />
    end<br />
</code></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Richard Forster</title>
		<link>http://ariejan.net/2007/01/28/ruby-sort-an-array-of-objects-by-an-attribute/comment-page-1/#comment-4918</link>
		<dc:creator>Richard Forster</dc:creator>
		<pubDate>Fri, 04 Jan 2008 05:07:06 +0000</pubDate>
		<guid isPermaLink="false">http://beta.ariejan.net/?p=77#comment-4918</guid>
		<description>There seem to be issues with using the database to do the sorting, as suggested by Ariejen  I am doing a find_by_sql with an 'order by' in it and the array returned is not in the same order as requested by 'order by'.  This is a JRuby app, so there may be other issues.  Sort by two fields fixes it.  The second field can be nil (a database test for null didn't help), so there is an extra test.  The point is the code in the block can be as simple or as complex as you like, which is what people seemed to be interested in.

subject_groups.sort! do &#124;a,b&#124; 
      a.team == nil ? akey = a.name.downcase : akey = (a.name +  a.team).downcase
      b.team == nil ? bkey = b.name.downcase : bkey = (b.name +  b.team).downcase
      akey  bkey       
    end</description>
		<content:encoded><![CDATA[<p>There seem to be issues with using the database to do the sorting, as suggested by Ariejen  I am doing a find_by_sql with an &#8216;order by&#8217; in it and the array returned is not in the same order as requested by &#8216;order by&#8217;.  This is a JRuby app, so there may be other issues.  Sort by two fields fixes it.  The second field can be nil (a database test for null didn&#8217;t help), so there is an extra test.  The point is the code in the block can be as simple or as complex as you like, which is what people seemed to be interested in.</p>
<p>subject_groups.sort! do |a,b|<br />
      a.team == nil ? akey = a.name.downcase : akey = (a.name +  a.team).downcase<br />
      b.team == nil ? bkey = b.name.downcase : bkey = (b.name +  b.team).downcase<br />
      akey  bkey<br />
    end</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ariejan de Vroom</title>
		<link>http://ariejan.net/2007/01/28/ruby-sort-an-array-of-objects-by-an-attribute/comment-page-1/#comment-3665</link>
		<dc:creator>Ariejan de Vroom</dc:creator>
		<pubDate>Thu, 25 Oct 2007 05:28:11 +0000</pubDate>
		<guid isPermaLink="false">http://beta.ariejan.net/?p=77#comment-3665</guid>
		<description>@James: you are correct. In this example you could use the database to do the sorting for you, which has way better performance than rails. 

The example, however, shows how to do it in Rails, in case you want to sort attributes that have a calculated value, for example.</description>
		<content:encoded><![CDATA[<p>@James: you are correct. In this example you could use the database to do the sorting for you, which has way better performance than rails. </p>
<p>The example, however, shows how to do it in Rails, in case you want to sort attributes that have a calculated value, for example.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: James</title>
		<link>http://ariejan.net/2007/01/28/ruby-sort-an-array-of-objects-by-an-attribute/comment-page-1/#comment-3664</link>
		<dc:creator>James</dc:creator>
		<pubDate>Thu, 25 Oct 2007 01:14:47 +0000</pubDate>
		<guid isPermaLink="false">http://beta.ariejan.net/?p=77#comment-3664</guid>
		<description>If this is for Rails, and the find makes it look like it might be, then this is another option:

User.find(:all, :order =&#62; "name")

Mysql sorting, at least, is case insensitive:

http://dev.mysql.com/doc/refman/5.0/en/case-sensitivity.html

That gives you sorting by more than one attribute too (:order =&#62; "name, age DESC, height").

Sorting by more than one attribute in straight Ruby would take some more thinking :)</description>
		<content:encoded><![CDATA[<p>If this is for Rails, and the find makes it look like it might be, then this is another option:</p>
<p>User.find(:all, :order =&gt; &#8220;name&#8221;)</p>
<p>Mysql sorting, at least, is case insensitive:</p>
<p><a href="http://dev.mysql.com/doc/refman/5.0/en/case-sensitivity.html" rel="nofollow" onclick="javascript:urchinTracker ('/outbound/comment/dev.mysql.com');">http://dev.mysql.com/doc/refman/5.0/en/case-sensitivity.html</a></p>
<p>That gives you sorting by more than one attribute too (:order =&gt; &#8220;name, age DESC, height&#8221;).</p>
<p>Sorting by more than one attribute in straight Ruby would take some more thinking :)</p>
]]></content:encoded>
	</item>
</channel>
</rss>
