title: Best way to sort the data : DB Query or in Application Code
body: <p>I have a Mysql table with some data (> million rows). I have a requirement to sort the data based on the below criteria</p>
  
  <p>1) Newest
  <p>2) Oldest
  <p>3) top rated
  <p>4) least rated</p>
  
  <p>What is the recommended solution to develop the sort functionality</p>
  
  <p>1) For every sort reuest execute a DBQuery with required joins and orderBy conditions and return the sorted data
  <p>2) Get all the data (un sorted) from table, put the data in cache. Write custom comparators (java) to sort the data. </p>
  
  <p>I am leaning towards #2 as the load on DB is only once. Moreover, application code is better than DBQuery.</p>
  
  <p>Please share your thoughts....</p>
  
  <p>Thanks,
  Karthik</p>