Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>First off: if you have a lot of objects/records you'll probably want to be doing the ordering in the database so that you can properly filter them out. This will have a couple advantages in most cases:</p> <p>1) You can limit the number of items you fetch <em>after</em> sorting them (meaning you don't have to load as many into memory)</p> <p>2) If you're using indexes properly, it will be very fast</p> <p>Ordering by multiple rows is straight forward, and explained in the answers to this question: <a href="https://stackoverflow.com/q/3587776/624590">Ruby on Rails: how do I sort with two columns using ActiveRecord?</a></p> <p>As far as the actual sorting goes, if you'd like to sort the array in memory the code you gave should work:</p> <pre><code>a.sort_by{|x| [x.rank, -x.created_at]} </code></pre> <p>Could you explain what the problem is? This seems like it sorts by the rank, and then by the time, as you specified. Where you looking to sort by another means (such as some score made up by the rank and how recent the item is)?</p> <p><strong>Edit:</strong></p> <p>Alright, so by your comment you wish to make a score that mixes rank with how recent the item is. This will be a tricky subject, for several reasons. First off, exactly how you model/score the results will be up to you, and you'll have to tweak it accordingly (so maybe it will be some fraction of <code>Time.now - item.created_at</code> multiplied by the rank, or something. It will basically be something you'll have to play with). </p> <p>On second thought, it might be better to just use the unix-style timestamp, rather than <code>Time.now - item.created_at</code>, because, unless somebody severely messes with your security, the higher the number, the more recent it will be. (Epoch issues notwithstanding). Plus this might make other things easier later on.</p> <p>For example, maybe it would be something like: </p> <p><code>rank*(item.created_at - 1300000000)/60000000</code>, which for the current time would result in <code>0.7453 * rank</code>, and for an item made a day from now would result in: <code>0.7467 * rank</code> (so a bit higher). Maybe your solution would be something more complex. It's really up to you to figure out an equation that works for your situation. </p> <p>The other issue is it might make your SQL queries and indexes a bit more complex (though you can index a table based on multiple columns with math (i.e. your scoring system) included; here's an example: <a href="http://use-the-index-luke.com/sql/where-clause/obfuscation/math" rel="nofollow noreferrer">http://use-the-index-luke.com/sql/where-clause/obfuscation/math</a>; this may seem a bit trickier for you because how recent it is will constantly be changing, so maybe it should just be a function of the timestamp itself (which increases with time, so recent items will inherently have higher values)). </p> <p>Basically, your first step will be figuring out exactly what sort of mathematical model best suits your needs. Experiment a bit. After that you'll just have to get things efficiently implemented up in Rails (correct database queries, indexes, etc.)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload