0

I am using this code-

myDB.rawQuery("SELECT * FROM " + TableName+ " ORDER BY "+ params[0], null);

to sort a table by columns and it works great except one of the columns is time (like in itunes) i.e. 2:35 for 2 minutes 35 seconds. This causes it to sort incorrectly. How can I sort it the proper way? I am using sqlite for android.

ghostbust555
  • 2,040
  • 16
  • 29

2 Answers2

1

Instead of storing the date as 2:35, you can have the column store seconds, and would thus be 155 (2 minutes, 35 seconds = 155 seconds). Thus it would sort it fine. You could translate seconds to minutes/seconds in the code.

Or, if you wanted to, you could have two columns, minutes and seconds.

It does not make sense to store time in a format like 2:35 if you are manipulating the data.

Dennis Sheil
  • 729
  • 5
  • 16
0

The best way is to store Time since epoch that is milliseconds since 01/01/1970 and you can then sort it however you like. Saving time like this lets you forget about worries of many issues that require effort.

Here it is an example.

Community
  • 1
  • 1
Muneeb Mirza
  • 810
  • 1
  • 17
  • 35