-1

I would like to compare two lists, if the value of the first list corresponds to the code of the second list, it would be necessary that the second list is sorted according to the values compared

First list :

      var List1 = [{id : "idValue2"}, {id : "idValue5"}]

Second List (before sorting)

     var List2 = [
                  {code : "idValue1", label: "value1"}, 
                  {code : "idValue2", label: "value2"},
                  {code : "idValue3", label: "value3"},
                  {code : "idValue4", label: "value4"};
                  {code : "idValue5", label: "value5"}]

After comparing list1 ids and list2 codes would get list2 sorted

Second List (after sorting)

     var List2 = [
                  {code : "idValue2", label: "value2"},
                  {code : "idValue5", label: "value5"}
                  {code : "idValue1", label: "value1"}, 
                  {code : "idValue3", label: "value3"},
                  {code : "idValue4", label: "value4"}]

Here idValue2 and idValue5 take the first places.

Do you have a solution ?

Greg-A
  • 772
  • 1
  • 19
  • 41
  • 3
    Possible duplicate of [Javascript - sort array based on another array](https://stackoverflow.com/questions/13304543/javascript-sort-array-based-on-another-array) – Björn Böing May 28 '19 at 11:38

3 Answers3

2

Try like this. just a filter and some will get you the desired arrays finally concat both arrays

var List1 = [{id : "idValue2"}, {id : "idValue5"}]

var List2 = [
   {code : "idValue1", label: "value1"}, 
   {code : "idValue2", label: "value2"},
   {code : "idValue3", label: "value3"},
   {code : "idValue4", label: "value4"},
   {code : "idValue5", label: "value5"}
]

var a = List2.filter(x=> ( List1.some(y=> { return y.id == x.code }) ));

var b = List2.filter(x=> ( !List1.some(y=> { return y.id == x.code }) ))


var list3 = a.concat(b);

console.log(list3);
Syed mohamed aladeen
  • 6,507
  • 4
  • 32
  • 59
1

This a simple, yet maybe not really performant solution. You basically have two nested for loops, which loop through the List1 and List2. If the id and the code are similar the element of List2 is pushed to the result array, and from a copy of the List2, this element is removed. Why a copy? So we don't modify the List2 while iterating over it. In the end the result (the sorted items) and copyList2 (the remaining, not sorted items) are joined together via concat.

var List1 = [{id : "idValue2"}, {id : "idValue5"}]

var List2 = [
   {code : "idValue1", label: "value1"}, 
   {code : "idValue2", label: "value2"},
   {code : "idValue3", label: "value3"},
   {code : "idValue4", label: "value4"},
   {code : "idValue5", label: "value5"}
]

var result = [];
var copyList2 = List2;

for(var i = 0; i < List1.length; i++){
  for(var j = 0; j < List2.length; j++){
   if(List1[i].id === List2[j].code){
     result.push(List2[j]);
     copyList2.splice(j, 1);
   }
  }
}

result = result.concat(copyList2);
console.log(result);
G43beli
  • 3,835
  • 4
  • 21
  • 28
0

You could store the ids in an temp-array as values. Then use a custom sort-function on the list2-array to compare the the positions of the code in the temp-array:

let list1 = [{id : "idValue2"}, {id : "idValue5"}];
let list2 = [
  {code : "idValue1", label: "value1"}, 
  {code : "idValue2", label: "value2"},
  {code : "idValue3", label: "value3"},
  {code : "idValue4", label: "value4"},
  {code : "idValue5", label: "value5"}
];

let tempArray = [];
for (let obj of list1) {
  tempArray.push(obj.id);
}

list2.sort(function(a, b) {
  let iA = tempArray.indexOf(a.code);
  let iB = tempArray.indexOf(b.code);
  if (iA == iB) {
      return 0;
  }
  if (iA == -1) {
    return 1;
  }
  if (iB == -1) {
    return -1;
  }
  if (iA < iB) {
    return -1;
  }
  if (iA > iB) {
    return 1;
  }
});


console.log(list2);
Jan
  • 2,853
  • 2
  • 21
  • 26