Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to sort data [duplicate]

In my Android application I have to fill an arraylist object with the data taken from a database. Also the data should be sorted. So i have two ways.

  1. Sort the data while taking from the database using SQL and put into array
  2. Get data from database and put them into the array and sorl the data later

out of these which is more efficient and less affecting the runtime. Need some help. Thank you!

like image 518
Samantha Withanage Avatar asked Dec 03 '13 09:12

Samantha Withanage


2 Answers

Sort the data while taking from the database using SQL and put into array

If you are able to sort data on database level, do it. Sorting on database level is much more faster as in memory - especially if your memory is limitated in the case of many records. Also performance can be increased by an usage of indexes which provide many databases.

So if you can and are able to sort in database, sort in database. If not, sort in application but here you are limitated by memory and sorting is significantly slower.

Note: It's worth to mention that if your sorting algorithm is very changeble during runtime (with high frequency and begins changing after data are retrieved from database) it's good approach to think about sorting in application and not in database and make some analysis focused on advantages and disadvatages due to application requirements (performance for instance).

like image 140
Simon Dorociak Avatar answered Oct 10 '22 00:10

Simon Dorociak


It depends on many things. But for general cases sorting from database is better as it may use indexings internally.

like image 40
stinepike Avatar answered Oct 10 '22 01:10

stinepike