基于javascript中的数字/对象排序数组或json

时间:2016-09-27 05:29:14

标签: javascript arrays json sorting

我面临着在JSON中排序的问题。我有一个可以重新排序的列表,并且可以保存重新排序,所以下次每当用户来时,他/她可以按照他安排的方式查看重新排序的列表。图片位于下方。First time reodering 现在它将对象保存在订单中,当他将其检索回来时,JSON就是以这种方式进行的。enter image description here,你可以看到对象ResourceSortedOrder是告诉我们将在哪个位置项目出现的命令选中的项只有其余的将有null。现在基于它我正在排序javascript在下面的数组/ JSON:

for (var i = 0; i < lst.length; i++) {
                  if (lst[i].ResourceSortedOrder != null) {
                   var temp = lst[i];
                  lst.splice(i, 1);
                  lst.splice(temp.ResourceSortedOrder, 0, temp);
                  lst.join();
                }


            }

它正在进行排序,但是由于上面的数据,第五个位置的第一个元素被放置在第4个位置,因此在JSON中100srvc资源低于它。所以我得到的结果如下图所示。 The Resulted One 而不是我的初始形象。请帮助

字符串中的JSON:

"[{"__type":"BusinessLayer.DTOApptResource","ResourceInClinicID":null,"ResourceInClinicName":null,"ResourceID":"d322a490-60ba-4739-a4ce-7d1de52f1789","ResourceName":"Dr. Maity","ResourceType":"Staff","ResourceRoster":[],"ResourceNotAvailableFrom":"0001-01-01T00:00:00.000Z","ResourceNotAvailableTo":"0001-01-01T00:00:00.000Z","ResourceSortedOrder":5,"ResourceKey":"Staff:d322a490-60ba-4739-a4ce-7d1de52f1789"},{"__type":"BusinessLayer.DTOApptResource","ResourceInClinicID":null,"ResourceInClinicName":null,"ResourceID":"e7217073-0763-4c42-8da0-7b4ce81f886a","ResourceName":"Dr. Shome","ResourceType":"Staff","ResourceRoster":[],"ResourceNotAvailableFrom":"0001-01-01T00:00:00.000Z","ResourceNotAvailableTo":"0001-01-01T00:00:00.000Z","ResourceSortedOrder":1,"ResourceKey":"Staff:e7217073-0763-4c42-8da0-7b4ce81f886a"},{"__type":"BusinessLayer.DTOApptResource","ResourceInClinicID":null,"ResourceInClinicName":null,"ResourceID":"670a9ec7-7502-4710-91d3-1c0dbe3023be","ResourceName":"TEST NEW DSHOME","ResourceType":"Staff","ResourceRoster":[],"ResourceNotAvailableFrom":"0001-01-01T00:00:00.000Z","ResourceNotAvailableTo":"0001-01-01T00:00:00.000Z","ResourceSortedOrder":2,"ResourceKey":"Staff:670a9ec7-7502-4710-91d3-1c0dbe3023be"},{"__type":"BusinessLayer.DTOApptResource","ResourceInClinicID":null,"ResourceInClinicName":null,"ResourceID":null,"ResourceName":null,"ResourceType":null,"ResourceRoster":null,"ResourceNotAvailableFrom":"0001-01-01T00:00:00.000Z","ResourceNotAvailableTo":"0001-01-01T00:00:00.000Z","ResourceSortedOrder":null,"ResourceKey":":"},{"__type":"BusinessLayer.DTOApptResource","ResourceInClinicID":null,"ResourceInClinicName":null,"ResourceID":"","ResourceName":"Hair care","ResourceType":"NonStaff","ResourceRoster":null,"ResourceNotAvailableFrom":"0001-01-01T00:00:00.000Z","ResourceNotAvailableTo":"0001-01-01T00:00:00.000Z","ResourceSortedOrder":null,"ResourceKey":"NonStaff:Hair care"},{"__type":"BusinessLayer.DTOApptResource","ResourceInClinicID":null,"ResourceInClinicName":null,"ResourceID":"","ResourceName":"New appointment","ResourceType":"NonStaff","ResourceRoster":null,"ResourceNotAvailableFrom":"0001-01-01T00:00:00.000Z","ResourceNotAvailableTo":"0001-01-01T00:00:00.000Z","ResourceSortedOrder":0,"ResourceKey":"NonStaff:New appointment"},{"__type":"BusinessLayer.DTOApptResource","ResourceInClinicID":null,"ResourceInClinicName":null,"ResourceID":"","ResourceName":"100 SRVC","ResourceType":"NonStaff","ResourceRoster":null,"ResourceNotAvailableFrom":"0001-01-01T00:00:00.000Z","ResourceNotAvailableTo":"0001-01-01T00:00:00.000Z","ResourceSortedOrder":null,"ResourceKey":"NonStaff:100 SRVC"},{"__type":"BusinessLayer.DTOApptResource","ResourceInClinicID":null,"ResourceInClinicName":null,"ResourceID":"","ResourceName":"Dressing","ResourceType":"NonStaff","ResourceRoster":null,"ResourceNotAvailableFrom":"0001-01-01T00:00:00.000Z","ResourceNotAvailableTo":"0001-01-01T00:00:00.000Z","ResourceSortedOrder":null,"ResourceKey":"NonStaff:Dressing"},{"__type":"BusinessLayer.DTOApptResource","ResourceInClinicID":null,"ResourceInClinicName":null,"ResourceID":"","ResourceName":"Hair care","ResourceType":"NonStaff","ResourceRoster":null,"ResourceNotAvailableFrom":"0001-01-01T00:00:00.000Z","ResourceNotAvailableTo":"0001-01-01T00:00:00.000Z","ResourceSortedOrder":null,"ResourceKey":"NonStaff:Hair care"},{"__type":"BusinessLayer.DTOApptResource","ResourceInClinicID":null,"ResourceInClinicName":null,"ResourceID":"","ResourceName":"smoothening","ResourceType":"NonStaff","ResourceRoster":null,"ResourceNotAvailableFrom":"0001-01-01T00:00:00.000Z","ResourceNotAvailableTo":"0001-01-01T00:00:00.000Z","ResourceSortedOrder":null,"ResourceKey":"NonStaff:smoothening"}]"

3 个答案:

答案 0 :(得分:3)

结果是

result <{1}}功能未被使用。

sort

答案 1 :(得分:1)

使用下面的代码而不是for循环来排序“lst”。

 lst.sort(function (a, b) {
    var a1 = a.ResourceSortedOrder, b1 = b.ResourceSortedOrder;
    a1 = a1 == null ? 99999 : a1;
    b1 = b1 == null ? 99999 : b1;


    return a1 - b1 ;
});

答案 2 :(得分:0)

此代码会将项目放在&#34; ResourceSortedOrder&#34;中指定的列表中。

var sortedList = [];
for (var i = 0; i < lst.length; i++) {
    var count = lst.length - sortedList.length;
    var item = null;
    for (var j = 0; j < count; j++) {
        if (lst[j].ResourceSortedOrder == i) {
            item = lst[j];
            break;
        }
    }
    if (item != null) {
        sortedList.push(item);
    }
    else {
        sortedList.push(lst[i]);
    }
}
lst = sortedList;