Q: Order SQLite results on FOR by geolocation in Cordova project My javascript is not properly ordering the locations according to the distance.
I need him to sort ASC form the places according to their geolocalidade.
What is wrong?
function calculateDistance(lat1, lon1, lat2, lon2)
{
    var radlat1 = Math.PI * lat1/180
    var radlat2 = Math.PI * lat2/180
    var radlon1 = Math.PI * lon1/180
    var radlon2 = Math.PI * lon2/180
    var theta = lon1-lon2
    var radtheta = Math.PI * theta/180
    var dist = Math.sin(radlat1) * Math.sin(radlat2) + Math.cos(radlat1) * Math.cos(radlat2) * Math.cos(radtheta);
    dist = Math.acos(dist)
    dist = dist * 180/Math.PI
    dist = dist * 60 * 1.1515
    dist = dist * 1.609344 * 1000
    return Math.round(dist)
}

function myfunction()
{
    db.transaction(function(transaction) {
        transaction.executeSql('SELECT * FROM places', [], function (tx, results) {

            for (i = 0; i < results.rows.length; i++)
                results.rows.item(i).distance = calculateDistance('MyLatitude', 'MyLongitude',results.rows.item(i).place_lat,results.rows.item(i).place_lng);

            results.rows.sort(function(a, b) { 
                return a.distance - b.distance;
            });

            for (i = 0; i < results.rows.length; i++)
            {
                var distance = '(Distance: ± '+calculateDistance('MyLatitude', 'MyLongitude', results.rows.item(i).place_lat, results.rows.item(i).place_lng)+' Meters)';
                console.log(i+' Place: '+results.rows.item(i).place_title+' - '+distance+'\n');
            }
        }, null);
    });
}

