通过对象内的数值对json数组进行排序

时间:2017-09-30 00:00:32

标签: javascript json sorting object numeric

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

/* not Singleton */ public class FooBarImpl implements Foo, Bar { /* ... */ }
// in your module:
@Binds @Singleton Foo bindFoo(FooBarImpl impl);
@Binds @Singleton Bar bindBar(FooBarImpl impl);
  

sort函数dosnt在这里工作

 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
            }
  

最终结果

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

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

}

  

继承了json的一点点

            //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>";
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

所以我找到了解决方案

感谢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);

            }
  

在外面排序

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

新循环

            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>";
            }
        }
    }
}

}`