i have a table e.g:
CREATE TABLE places (id UNIQUE, name, latitude, longitude)
i then loop through my dataset and calculate the distance between users location and my rows, then i display as a list in HTML.
My problem is that i dont know how to sort this list out by ascending order of distance.
Do i save the distance back into the Places table and then do another Select * with an Ascending order…?
can i not do something like select my results, but order them by running a javascript function. basically something like this:
tx.executeSql('select * from places order by '
function getdistance(userLat, UserLong, places.latitude, places.longitude)
thanks,
Because you calling a function that you want in Javascript, you cannot use a function defined external to the database engine as a comparison operator, especially if used via the syntax you provide, where it is being evaluated externally from the query – you need the data to find the value to evaluate the order that the data should be returned. There is no way for the data to get anywhere!
Instead, you could create a table that has an extra, empty column, and then calculate it via Javascript on the client side, in the HTML. This would allow multiple users to use the application, and you can sort it there.
OF course, if you are comfortable with doing so, you can create or define a C function in SQLite – I am unfamiliar with the process, but it is described in their documentation here. This would allow you to call it in the SQL itself;
Select *, Distance(UserLat, UserLong, places.latitude, places.longitude) as dist from places, uservalues order by distAlternatively, as suggested, you could save the data back into the database, but this will limit concurrency.
Lastly, you could use MySQL or another database system that is more fully featured.