按对象中的数值对 json 数组进行排序

Sorting json array by a numeric value in an object within

我正在寻找一种方法来根据另一个名为 haversine 的库计算的距离 (dist) 对 json 文件中的数组进行排序。该应用程序将根据选择找到某个值附近的任何地理位置。因此,我希望最接近的结果首先出现。

 function map(position){
    var obj,
        xmlhttp,
        i;

xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET", "parking_data.json", true);
xmlhttp.send();

xmlhttp.onload = function() {

    if (this.readyState == 4 && this.status == 200) {
        obj = JSON.parse(this.responseText);
        console.log(obj);

        //var features = obj.features;
        var features = obj.features;

        for (i in features) {

            var street = features[i].properties.Vejnavn;

            var destX = features[i].geometry.coordinates[0][0][0];
            var destY = features[i].geometry.coordinates[0][0][1];

            var x = position.coords.latitude;
            var y = position.coords.longitude;

            //haversine coords to calculate
            const start = {
                    latitude: x,
                    longitude: y
            }

            const end = {
                latitude: destY,
                longitude: destX
            }

The sort function dosnt work here

            features[i].dist = haversine(start, end);

            features.sort(function(a, b){
                return a.dist - b.dist;
            });

the final results

            //results
            var distanceDisplay = Math.floor(features[i].dist * 10) / 10;

            var avaidableSpots = features[i].dist <= searchRadius;

            if (avaidableSpots) {
                var href = '"location_id/' + [i] + '.php"';
                    ul.innerHTML += `<li>` + "<a href=" + href + ">" + `${street} <br><small>` + distanceDisplay + " km. away</small></a> </li>";
                }
            }
            //loading animation
            document.getElementById('loading').style.display = "none";

            if(!ul.children[0]){
                ul.innerHTML = "<li>No results found</li>";
            }
        }
    }
}

}

heres a bit of the json

{
"type": "FeatureCollection",
"crs": {
    "type": "name",
    "properties": {
        "name": "urn:ogc:def:crs:OGC:1.3:CRS84"
    }
},

"features": [{
        "type": "Feature",
        "properties": {
            "Vejnavn": "Nørreport 22",
            "Bemrk": "Døgn åben, Max højde 2meter",
            "B_tidsrum_hverdage": "9-19",
            "B_tidsrum_loerdage": "9-16",
            "Pris": {
                "første_time": "12",
                "anden_time": "17",
                "derefter": "22",
                "Handicap": "Gratis"
            }
        },
        "geometry": {
            "type": "Polygon",
            "coordinates": [
                [
                    [10.21198430766165, 56.161661523678212],
                    [10.21266345366759, 56.162109347345826],
                    [10.213379083709297, 56.161875171331005],
                    [10.212971865398755, 56.16161005348296],
                    [10.21269274566342, 56.16145985967453],
                    [10.212264238263865, 56.161310950838008],
                    [10.21198430766165, 56.161661523678212]
                ]
            ]
        }
    },
    {
        "type": "Feature",
        "properties": {
            "Vejnavn": "Christiansgade",
            "Bemrk": "Sejr Gade, J.M. Mørks Gade",
            "B_tidsrum_hverdage": "9-19",
            "B_tidsrum_loerdage": "9-16",
            "Pris": {
                "første_time": "12",
                "anden_time": "17",
                "derefter": "22"
            }
        },

        "geometry": {
            "type": "Polygon",
            "coordinates": [
                [
                    [10.203493342206118, 56.15576224055593],
                    [10.20349483343378, 56.155809938868558],
                    [10.203749188759666, 56.155855444112227],
                    [10.203776430326384, 56.155779251571253],
                    [10.203744441672518, 56.155703635937179],
                    [10.203695574311467, 56.155654601963974],
                    [10.203021738412547, 56.155096606059999],
                    [10.203729625724721, 56.154848812884772],
                    [10.203613464554529, 56.154727741852483],
                    [10.202905524413632, 56.154978949366964],
                    [10.201342143398271, 56.153706718560613],
                    [10.201140828364331, 56.153771393904613],
                    [10.201809128098617, 56.154339337761684],
                    [10.201049816630045, 56.154647013638311],
                    [10.20046875891518, 56.155167706035151],
                    [10.200718751207759, 56.155218112010779],
                    [10.201192871387885, 56.154763422771346],
                    [10.201950235800657, 56.154455225835676],
                    [10.202644304270883, 56.155058766641652],
                    [10.201689109197018, 56.155419928956171],
                    [10.201981088837139, 56.155489782008686],
                    [10.202808379456766, 56.155188178014221],
                    [10.203493342206118, 56.15576224055593]
                ]
            ]
        }
    },
    {
        "type": "Feature",
        "properties": {
            "Vejnavn": "Fredensgade",
            "Bemrk": "",
            "B_tidsrum_hverdage": "9-19",
            "B_tidsrum_loerdage": "9-16",
            "Pris": {
                "første_time": "12",
                "anden_time": "17",
                "derefter": "22"
            }
        },
        "geometry": {
            "type": "Polygon",
            "coordinates": [
                [
                    [10.209425693995254, 56.154197914205049],
                    [10.21030062816121, 56.154011346528499],
                    [10.211129977606817, 56.154423023309761],
                    [10.211327657776996, 56.15431487753235],
                    [10.21068026669629, 56.154011851218755],
                    [10.210310693368099, 56.153850138319278],
                    [10.210227564797131, 56.153863711780382],
                    [10.209540656720925, 56.153993357891729],
                    [10.209425693995254, 56.154197914205049]
                ]
            ]
        }
    },
    {
        "type": "Feature",
        "properties": {
            "Vejnavn": "Vestrebro Torv",
            "Bemrk": "",
            "B_tidsrum_hverdage": "9-19",
            "B_tidsrum_loerdage": "9-16",
            "Pris": {
                "første_time": "12",
                "anden_time": "17",
                "derefter": "22",
                "Handicap": "Gratis"
            }
        },

        "geometry": {
            "type": "Polygon",
            "coordinates": [
                [
                    [10.198363753421004, 56.15811703573435],
                    [10.199465352885191, 56.158343296194474],
                    [10.199505123304126, 56.158276148255112],
                    [10.199371166483791, 56.158203947215895],
                    [10.199021443238632, 56.158110656973257],
                    [10.198489959077255, 56.157945537532974],
                    [10.198363753421004, 56.15811703573435]
                ]
            ]
        }
    },
    {
        "type": "Feature",
        "properties": {
            "Vejnavn": "Telefonsmøgen",
            "Bemrk": "Busgaden,  Døgn åben, Max højde 2meter",
            "B_tidsrum_hverdage": "9-19",
            "B_tidsrum_loerdage": "9-16",
            "Pris": {
                "første_time": "12",
                "anden_time": "17",
                "derefter": "22",
                "Handicap": "Gratis"
            }
        },

        "geometry": {
            "type": "Polygon",
            "coordinates": [
                [
                    [10.205504416747992, 56.154708772190681],
                    [10.205850484056896, 56.154603590643134],
                    [10.206336627248689, 56.155094754544855],
                    [10.206539130631272, 56.15525811043085],
                    [10.206418810269692, 56.155297473274125],
                    [10.206598514570207, 56.155452515421111],
                    [10.206086829985603, 56.155605770267059],
                    [10.205824683331402, 56.155481183845794],
                    [10.206231572692868, 56.155379810710912],
                    [10.206028273544995, 56.155191033207885],
                    [10.205504416747992, 56.154708772190681]
                ]
            ]
        }
    },
    {
        "type": "Feature",
        "properties": {
            "Vejnavn": "Toldbodgade",
            "Bemrk": "",
            "B_tidsrum_hverdage": "9-19",
            "B_tidsrum_loerdage": "9-16",
            "Pris": {
                "første_time": "12",
                "anden_time": "17",
                "derefter": "22",
                "Handicap": "Gratis"
            }
        },

        "geometry": {
            "type": "Polygon",
            "coordinates": [
                [
                    [10.21114015945408, 56.151973032129128],
                    [10.211127797384513, 56.151820579782836],
                    [10.21192661502328, 56.151872142618217],
                    [10.21216363265917, 56.151924809185516],
                    [10.212736712080071, 56.152516811292486],
                    [10.212526515441951, 56.152595160570797],
                    [10.211937476500699, 56.152048421148152],
                    [10.211891442317905, 56.152028205975071],
                    [10.211837559554287, 56.1520194793125],
                    [10.21114015945408, 56.151973032129128]
                ]
            ]
        }
    },
    {
        "type": "Feature",
        "properties": {
            "Vejnavn": "Kalkværksvej",
            "Bemrk": "",
            "B_tidsrum_hverdage": "9-19",
            "B_tidsrum_loerdage": "9-16"
        },
        "geometry": {
            "type": "Polygon",
            "coordinates": [
                [
                    [10.212480714745833, 56.149075635647279],
                    [10.21189917631437, 56.149174609469419],
                    [10.211916552728256, 56.149225476629084],
                    [10.2114262824156, 56.149570025000301],
                    [10.211792830540057, 56.150097114018131],
                    [10.212031174394054, 56.149957029103277],
                    [10.212232911890748, 56.149799601402144],
                    [10.212456271956359, 56.14956208022889],
                    [10.212539320660754, 56.149423607705977],
                    [10.212515682718251, 56.149301726856663],
                    [10.21258299079263, 56.149292170787348],
                    [10.212480714745833, 56.149075635647279]
                ]
            ]
        }
    },
    {
        "type": "Feature",
        "properties": {
            "Vejnavn": "Sct. Nicolaus Gade",
            "Bemrk": "",
            "B_tidsrum_hverdage": "9-19",
            "B_tidsrum_loerdage": "9-16"
        },
        "geometry": {
            "type": "Polygon",
            "coordinates": [
                [
                    [10.198777516530196, 56.151549950936577],
                    [10.19864861812256, 56.15143259305345],
                    [10.199413442807074, 56.15116378511501],
                    [10.19954800186091, 56.151287107531601],
                    [10.198777516530196, 56.151549950936577]
                ]
            ]
        }
    },
    {
        "type": "Feature",
        "properties": {
            "Vejnavn": "Guldsmedegade",
            "Bemrk": "",
            "B_tidsrum_hverdage": "9-19",
            "B_tidsrum_loerdage": "9-16"
        },
        "geometry": {
            "type": "Polygon",
            "coordinates": [
                [
                    [10.207712385645939, 56.160222265146579],
                    [10.207955983250434, 56.160236867139538],
                    [10.207838277771101, 56.158990025831613],
                    [10.207589123601817, 56.15895202586487],
                    [10.207712385645939, 56.160222265146579]
                ]
            ]
        }
    },
    {
        "type": "Feature",
        "properties": {
            "Vejnavn": "Borgergade",
            "Bemrk": "",
            "B_tidsrum_hverdage": "9-19",
            "B_tidsrum_loerdage": "9-16"
        },
        "geometry": {
            "type": "Polygon",
            "coordinates": [
                [
                    [10.208613304002355, 56.15905361427231],
                    [10.208703310008385, 56.16033540976089],
                    [10.208939608287107, 56.160358527543863],
                    [10.208864488719254, 56.159048551920868],
                    [10.208841877277177, 56.159051468720762],
                    [10.208617169985862, 56.159053666321903],
                    [10.208613304002355, 56.15905361427231]
                ]
            ]
        }
    },
    {
        "type": "Feature",
        "properties": {
            "Vejnavn": "Paradisgade",
            "Bemrk": "",
            "B_tidsrum_hverdage": "9-19",
            "B_tidsrum_loerdage": "9-16"
        },
        "geometry": {
            "type": "Polygon",
            "coordinates": [
                [
                    [10.210099119578143, 56.159554662033827],
                    [10.209333209390287, 56.160519560313936],
                    [10.209511089093091, 56.16066661907923],
                    [10.210291698988216, 56.159641822384678],
                    [10.210099119578143, 56.159554662033827]
                ]
            ]
        }
    },
    {
        "type": "Feature",
        "properties": {
            "Vejnavn": "Rosenkrantzgade",
            "Bemrk": "Ny Banegårdsgade",
            "B_tidsrum_hverdage": "9-19",
            "B_tidsrum_loerdage": "9-16"
        },
        "geometry": {
            "type": "Polygon",
            "coordinates": [
                [
                    [10.204880225968527, 56.150763047806443],
                    [10.205269212469727, 56.150780370504407],
                    [10.206030469030146, 56.150898021477737],
                    [10.206732968068268, 56.151010400993002],
                    [10.207137521967438, 56.151149319052635],
                    [10.20747999111166, 56.151351471224004],
                    [10.207796603656663, 56.15175047744745],
                    [10.206423886404957, 56.152063641332049],
                    [10.205652778892048, 56.152125708439947],
                    [10.205185334218069, 56.152158392441876],
                    [10.205212404494107, 56.152298032291021],
                    [10.206488410112993, 56.152185932745233],
                    [10.207911191501694, 56.151903368598816],
                    [10.208238324862734, 56.152416745868514],
                    [10.208509870893637, 56.152359099588296],
                    [10.208183671844054, 56.151844815333916],
                    [10.207834500726626, 56.151306134534096],
                    [10.207495326256003, 56.151085890330357],
                    [10.206900155182172, 56.150854038382796],
                    [10.205889939702651, 56.150692636999608],
                    [10.205197354423486, 56.150613851586705],
                    [10.204847543085153, 56.150598303249318],
                    [10.204880225968527, 56.150763047806443]
                ]
            ]
        }
    },
    {
        "type": "Feature",
        "properties": {
            "Vejnavn": "Klostergade",
            "Bemrk": "",
            "B_tidsrum_hverdage": "9-19",
            "B_tidsrum_loerdage": "9-16"
        },
        "geometry": {
            "type": "Polygon",
            "coordinates": [
                [
                    [10.20275679168377, 56.158545649945147],
                    [10.202864171910921, 56.158570932327251],
                    [10.204241427314328, 56.158919455114514],
                    [10.204490430696714, 56.158921880841604],
                    [10.205376085080399, 56.158883235023119],
                    [10.205361650694117, 56.158730802585502],
                    [10.204347181968254, 56.158779509362581],
                    [10.203907896403479, 56.158659789753671],
                    [10.202878927738498, 56.158414531510168],
                    [10.20275679168377, 56.158545649945147]
                ]
            ]
        }
    },

所以我找到了解决方案

感谢 Jakub Piskorz ` 谢谢^^

function map(position){
    var obj,
        xmlhttp,
        i;

xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET", "parking_data.json", true);
xmlhttp.send();

xmlhttp.onload = function() {

    if (this.readyState == 4 && this.status == 200) {
        obj = JSON.parse(this.responseText);

        //var features = obj.features;
        var features = obj.features;
        var dbList = [];
        var distanceBetween = [];
        for (i in features) {

            var street = features[i].properties.Vejnavn;

            var destX = features[i].geometry.coordinates[0][0][0];
            var destY = features[i].geometry.coordinates[0][0][1];

            var x = position.coords.latitude;
            var y = position.coords.longitude;

            //haversine coords to calculate
            const start = {
                    latitude: x,
                    longitude: y
            }

            const end = {
                latitude: destY,
                longitude: destX
            }

            features[i].dist = haversine(start, end);

            }

sort outside

            features.sort(function(a, b){
                return a.dist - b.dist;
            });

new loop

            for (var x in features) {
                var street = features[x].properties.Vejnavn;
                var distanceDisplay = Math.floor(features[x].dist * 10) / 10;
                var avaidableSpots = features[x].dist <= searchRadius;

                if (avaidableSpots) {
                    var href = '"location_id/' + [x] + '.php"';
                        ul.innerHTML += `<li>` + "<a href=" + href + ">" + `${street} <br><small>` + distanceDisplay + " km. away</small></a> </li>";
                    }
            }

            //loading animation
            document.getElementById('loading').style.display = "none";

            if(!ul.children[0]){
                ul.innerHTML = "<li>No results found</li>";
            }
        }
    }
}

}`