-1

I have one array and other array for sort order. Now i want to sort the array according to the sort array items.

    var checkedIds =["2","1","4"];//sort order array
    
   //other array 
    var data1= {
        "ProductImages": [
                   { "checkBoxField": false, "itemId": 1, "rateValue":"$34" },
                   { "checkBoxField": false, "itemId": 2, "rateValue":"$34" },
                   { "checkBoxField": false, "itemId": 3, "rateValue":"$34" },
                   { "checkBoxField": false, "itemId": 4, "rateValue":"$34" },
                   { "checkBoxField": false, "itemId": 5, "rateValue":"$34" },
                   { "checkBoxField": false, "itemId": 6, "rateValue":"$34" },
                   { "checkBoxField": false, "itemId": 7, "rateValue":"$34" },
                   { "checkBoxField": false, "itemId": 8,"rateValue":"$34" }
        ]
    };

Output Should be

var data1= {
        "ProductImages": [
                   { "checkBoxField": true, "itemId": 2, "rateValue":"$34" },
                   { "checkBoxField": true, "itemId": 1, "rateValue":"$34" },
                   { "checkBoxField": true, "itemId": 4, "rateValue":"$34" },
                   { "checkBoxField": false, "itemId": 3, "rateValue":"$34" },
                   { "checkBoxField": false, "itemId": 5, "rateValue":"$34" },
                   { "checkBoxField": false, "itemId": 6, "rateValue":"$34" },
                   { "checkBoxField": false, "itemId": 7, "rateValue":"$34" },
                   { "checkBoxField": false, "itemId": 8,"rateValue":"$34" }
        ]
    };
5
  • Have you tried anything? Also, why are you storing the numbers as strings in checkedIds? Commented Jun 20, 2016 at 14:15
  • What have you tried so far? How should the elements be sorted that are not part of the checkedIds array? Commented Jun 20, 2016 at 14:16
  • I think there's a better way to solve what you're trying to do than doing this. See What is the XY Problem?. Why don't you make ProductImages an object instead and make the itemId the key? You can use checkedIds to reference the keys when you need them. Commented Jun 20, 2016 at 14:23
  • do you need the "checkBoxField": true, as well? Commented Jun 20, 2016 at 14:29
  • You seem to be confusing JavaScript and JSON Commented Feb 2, 2023 at 13:22

5 Answers 5

1

You can have a custom sort based on calculated rank:

JSFiddle.

 var checkedIds = ["2", "1", "4"]; //sort order array

 //json array 
var jsonData1={ProductImages:[{checkBoxField:!1,itemId:1,rateValue:"$34"},{checkBoxField:!1,itemId:2,rateValue:"$34"},{checkBoxField:!1,itemId:3,rateValue:"$34"},{checkBoxField:!1,itemId:4,rateValue:"$34"},{checkBoxField:!1,itemId:5,rateValue:"$34"},{checkBoxField:!1,itemId:6,rateValue:"$34"},{checkBoxField:!1,itemId:7,rateValue:"$34"},{checkBoxField:!1,itemId:8,rateValue:"$34"}]};

function getRank(item){
  var index = checkedIds.indexOf(item.itemId.toString());
  var rank = 1;
  if (index>-1){
    rank *= (checkedIds.length - index) * -1;
  }
  return rank;
}

jsonData1.ProductImages.sort(function(a,b){
  var r1 = getRank(a);
  var r2 = getRank(b);
  return r1>r2 ? 1 : r1<r2 ?-1: 0;
})

document.getElementById("result").innerHTML =  JSON.stringify(jsonData1.ProductImages.map(function(a){return a.itemId}));
<pre id="result"></pre>

Sign up to request clarification or add additional context in comments.

Comments

1

The idea is to write an own compare function for the sort function in JS: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort

In this compare function you can also set the "checkBoxField" value directly. (Might be not the most efficient way)

This is an example for your problem:

var checkedIds =[2,1,4];//sort order array

   //json array 
var jsonData1= {
  "ProductImages": [
    { "checkBoxField": false, "itemId": 1, "rateValue":"$34" },
    { "checkBoxField": false, "itemId": 2, "rateValue":"$34" },
    { "checkBoxField": false, "itemId": 3, "rateValue":"$34" },
    { "checkBoxField": false, "itemId": 4, "rateValue":"$34" },
    { "checkBoxField": false, "itemId": 5, "rateValue":"$34" },
    { "checkBoxField": false, "itemId": 6, "rateValue":"$34" },
    { "checkBoxField": false, "itemId": 7, "rateValue":"$34" },
    { "checkBoxField": false, "itemId": 8,"rateValue":"$34" }
  ]
};

var arr = jsonData1["ProductImages"]
arr.sort(function compare(a, b) {
  if (checkedIds.indexOf(a["itemId"]) >= 0) {
    a["checkBoxField"] = true;
  }
  if (checkedIds.indexOf(b["itemId"]) >= 0) {
    b["checkBoxField"] = true;
  }
    if (checkedIds.indexOf(a["itemId"]) >= 0 && checkedIds.indexOf(b["itemId"]) < 0) {
      return -1;
  } 
  if (checkedIds.indexOf(b["itemId"]) >= 0 && checkedIds.indexOf(a["itemId"]) < 0) {
      return 1;
  } 
    return checkedIds.indexOf(a["itemId"])-checkedIds.indexOf(b["itemId"]);
 });

console.log(arr);

As mentioned in one comment you should use integers in your checkedIds array.

https://jsfiddle.net/yxq0xL6L/1/

Comments

1

You could use a hash table for the order and a default value, big enough to move not prioritized values to the end.

var checkedIds = ["2", "1", "4"],
    jsonData1 = { "ProductImages": [{ "checkBoxField": false, "itemId": 1, "rateValue": "$34" }, { "checkBoxField": false, "itemId": 2, "rateValue": "$34" }, { "checkBoxField": false, "itemId": 3, "rateValue": "$34" }, { "checkBoxField": false, "itemId": 4, "rateValue": "$34" }, { "checkBoxField": false, "itemId": 5, "rateValue": "$34" }, { "checkBoxField": false, "itemId": 6, "rateValue": "$34" }, { "checkBoxField": false, "itemId": 7, "rateValue": "$34" }, { "checkBoxField": false, "itemId": 8, "rateValue": "$34" }] };

jsonData1.ProductImages.sort(function (hash) {
    checkedIds.forEach(function (a, i) {
        hash[a] = i + 1;
    });
    return function (a, b) {
        return (hash[a.itemId] || Infinity) - (hash[b.itemId] || Infinity);
    };
}(Object.create(null)));

console.log(jsonData1);

Version with updating checkBoxField and short circuiting.

var checkedIds = ["2", "1", "4"],
    jsonData1 = { "ProductImages": [{ "checkBoxField": false, "itemId": 1, "rateValue": "$34" }, { "checkBoxField": false, "itemId": 2, "rateValue": "$34" }, { "checkBoxField": false, "itemId": 3, "rateValue": "$34" }, { "checkBoxField": false, "itemId": 4, "rateValue": "$34" }, { "checkBoxField": false, "itemId": 5, "rateValue": "$34" }, { "checkBoxField": false, "itemId": 6, "rateValue": "$34" }, { "checkBoxField": false, "itemId": 7, "rateValue": "$34" }, { "checkBoxField": false, "itemId": 8, "rateValue": "$34" }] },
    hash = Object.create(null);

checkedIds.forEach(function (a, i) {
    hash[a] = i + 1;
});

jsonData1.ProductImages.some(function (a, i) {
    if (a.itemId in hash) {
        a.checkBoxField = true;
        return !--this.count;
    }
}, { count: checkedIds.length });

jsonData1.ProductImages.sort(function (a, b) {
    return (hash[a.itemId] || Infinity) - (hash[b.itemId] || Infinity);
});

console.log(jsonData1);

Comments

0

Here is a boring, non-clever, but easy-to-understand iterative solution:

var sortMe = function(ids, data) {
  var i,j,ret = [];

  // first get the items with matched ids:
  for (i=0; i<ids.length;i++) {
     for (j=0; j<data.length; j++) {
      if (data[j].itemId == ids[i]) {
        ret.push(data[j]);
      }
    }
  }
  // now get everything else (in its original order):
  for (i=0; i<data.length; i++) {
    // note: ids[] contains strings, so need to add "" to each data.itemId:
     if (ids.indexOf(data[i].itemId+"") == -1) { 
       ret.push(data[i]);
     }
  }
  return ret;
};

var checkedIds = ["2", "1", "4"]; //sort order array
var jsonData1 = {
  "ProductImages": [{
    "checkBoxField": false,
    "itemId": 1,
    "rateValue": "$34"
  }, {
    "checkBoxField": false,
    "itemId": 2,
    "rateValue": "$34"
  }, {
    "checkBoxField": false,
    "itemId": 3,
    "rateValue": "$34"
  }, {
    "checkBoxField": false,
    "itemId": 4,
    "rateValue": "$34"
  }, {
    "checkBoxField": false,
    "itemId": 5,
    "rateValue": "$34"
  }, {
    "checkBoxField": false,
    "itemId": 6,
    "rateValue": "$34"
  }, {
    "checkBoxField": false,
    "itemId": 7,
    "rateValue": "$34"
  }, {
    "checkBoxField": false,
    "itemId": 8,
    "rateValue": "$34"
  }]
};

var foo = sortMe(checkedIds, jsonData1.ProductImages);
console.log(foo);

Comments

-1

It seems you want a sort of selection sort with the distinction that instead of looking for a minimum value, you look for the next value in your list.

Comments

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.