简体   繁体   中英

sort array or json on basis of a number/object in javascript

I am facing an issue with sorting in JSON.I have a list which can reorder and the reordering can be saved so next time whenever user will come he/she can see the reordered list the way he arranged. The image is below. 第一次重排 Now it get save in object the order and when he retrieve it back the JSON is coming in this way. 在此处输入图片说明 ,you can see object ResourceSortedOrder is the order which telling that on which position item will be present it will come for the checked item only the rest will have null.Now on the basis of it I am sorting the array/JSON the javascript is below:

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();
                }


            }

It is getting sorted but the first element which has to place at 5th position is getting placed at the 4th position because of the data above it 100srvc resources is below it in JSON. So I am getting a result like below image. 结果一 Instead of my initial image.Please help

the JSON in string:

"[{"__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"}]"

result is the result.
sort function is not used .

var checked = [];
var unchecked = [];

lst.map(function(item){
    if(item.ResourceSortedOrder !== null){
        checked.push(item);
    }else{
        unchecked.push(item);
    }
});

var result = [];

checked.map(function(item){
    result[item.ResourceSortedOrder] = item;
});

var index = 0;

unchecked.map(function(item){   
    while(result[index]){
        index ++;
    }
    result[index] = item;   
});

Use below code instead of for loop to sort "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 ;
});

This code will place the item in list as specified in "ResourceSortedOrder".

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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