简体   繁体   English

Javascript排序数组-在compare函数中调用函数

[英]Javascript sort array - Calling a function inside compare function

I have an array of objects that contain some address data including latitude and longitude. 我有一个对象数组,其中包含一些地址数据,包括纬度和经度。

I have a function that calculates the distance between two sets of lat/long coordinates. 我有一个计算两组纬度/经度坐标之间距离的函数。

I need to sort my array from closest to farthest from a specific set of lat/long. 我需要从一组特定的经/纬度中,从最接近到最远对我的数组进行排序。

I'm trying to use arr.sort(comparefunction) but no luck so far. 我正在尝试使用arr.sort(comparefunction)但到目前为止没有运气。 Is it possible to use an external function inside my compare function to do it? 是否可以在我的比较函数中使用外部函数来做到这一点?

Here is the code I tried. 这是我尝试的代码。

function getDistance (lat1, lon1, lat2, lon2, unit) {
    var radlat1 = Math.PI * lat1/180
    var radlat2 = Math.PI * lat2/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);
    if (dist > 1) {
        dist = 1;
    }
    dist = Math.acos(dist)
    dist = dist * 180/Math.PI
    dist = dist * 60 * 1.1515
    if (unit=="K") { dist = dist * 1.609344 }
    if (unit=="N") { dist = dist * 0.8684 }
    return dist
}


let distributors = [
    {
        city: "A",
        lat: 44.3384925,
        long: -59.6993207,
        name: "Name 1",
    },
    {
        city: "B",
        lat: 44.3384925,
        long: -78.6993207,
        name: "Name 2",
    },
    {
        city: "C",
        lat: 64.3384925,
        long: -39.6993207,
        name: "Name 3",
    }
];

let sortedDist = distributors.sort(function(a, b){ 

    if(getDistance(44.3617171302915, -79.65860486970848, a.lat, a.long, "K") < getDistance(44.3617171302915, -79.65860486970848, a.lat, a.long, "K")){
        return -1;
    }
    if(getDistance(44.3617171302915, -79.65860486970848, a.lat, a.long, "K") > getDistance(44.3617171302915, -79.65860486970848, a.lat, a.long, "K")){
        return 1;
    }
    return 0;
})


So in this scenario, the second element in the array is the closest in distance from 44.3617171302915, -79.65860486970848 , but I console.log(sortedDist) and nothing has changed. 因此,在这种情况下,数组中的第二个元素距离44.3617171302915, -79.65860486970848 ,但是我console.log(sortedDist)并没有任何改变。

The problem is that you're comparing a against a , try: 问题是您正在将aa进行比较,请尝试:

let sortedDist = distributors.sort(function(a, b){ 

    if(getDistance(44.3617171302915, -79.65860486970848, a.lat, a.long, "K") < getDistance(44.3617171302915, -79.65860486970848, b.lat, b.long, "K")){
        return -1;
    }
    if(getDistance(44.3617171302915, -79.65860486970848, a.lat, a.long, "K") > getDistance(44.3617171302915, -79.65860486970848, b.lat, b.long, "K")){
        return 1;
    }
    return 0;
})

or the same with shorter syntax: 或使用较短的语法相同:

let sortedDist = distributors.sort(function(a, b){ 

    let aDist = getDistance(44.3617171302915, -79.65860486970848, a.lat, a.long, "K");
    let bDist = getDistance(44.3617171302915, -79.65860486970848, b.lat, b.long, "K");

    return aDist - bDist;
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM