0

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.First time reodering Now it get save in object the order and when he retrieve it back the JSON is coming in this way.enter image description here,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. The Resulted One 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"}]"
9
  • Why don't you use sort function to do it ? Commented Sep 27, 2016 at 5:41
  • I don't know how to use it I have tried but not getting how to use it Commented Sep 27, 2016 at 5:43
  • I already post the image for json and how you want it,tell me will post it Commented Sep 27, 2016 at 6:00
  • Have added that Commented Sep 27, 2016 at 6:08
  • yes as I am only setting that is there any way that i can set it anything else as data which are checked in the list having sorted value rest are null Commented Sep 27, 2016 at 6:21

3 Answers 3

3

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;   
});
Sign up to request clarification or add additional context in comments.

Comments

1

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

5 Comments

Not working data Dr.Maity moving to two places up now
I have tested results with your sample data. Dr matty have ResourceSortedOrder: 5. so its showing on 4th position. in 0,1,2,5 and so on.....
Yes but it should show on fifth postion and it is showing in 3rd position according to your code as list is starting from 0,thanks for helping if you get any solution please help me
Have you got default sort order. Suppose no item is checked then how will list be rearranged?
ya by default it is coming in some order but at that time resorcesorted object is nothing 0 it is getting value in client side only
0

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;

3 Comments

thanks a lot buddy for help but it is not working the issue is because of null ones only...have to do something else by the way thanks
Best approach will be to save order of every item in a list. This is messing because order of item have null "ResourceSortedOrder" is unspecified
Also in sample json list there are some item having all values null, e.g ResourceName, ResourceType, ResourceID

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.