0

I want to Sort an array in JavaScript based on another JavaScript array.

First arr = ["Name", "Address1", "Address2", "City", "State", "Zip Code", "Formatted Address"];

Second arr = [Name: "kd", State: "test", Address1: "tt"]

Now I want to sort the second arr in the order of the first arr. I want this type of result:

[Name: "kd", Address1: "tt", State: "test"]
NightOwl888
  • 55,572
  • 24
  • 139
  • 212
  • Possible duplicate of [Javascript - sort array based on another array](https://stackoverflow.com/questions/13304543/javascript-sort-array-based-on-another-array) – DragonBorn Feb 22 '18 at 05:36
  • 1
    second one looks more like an Object, not an array. It has to be initialized as var second = {Name: "kd", State: "test", Address1: "tt"}; – Jayanth Feb 22 '18 at 05:55

5 Answers5

0

You can sort second array by itrating over first array as below:

Firstarr = ["Name", "Address1", "Address2", "City", "State", "Zip Code", "Formatted Address"];
Secondarr = {Name: "kd", State: "test", Address1: "tt"};

var finalArr = {};

$.each(Firstarr, function(i, value){
      if(typeof Secondarr[value] !== "undefined"){
         finalArr[value] =  Secondarr[value];
      }
});
Ishpreet
  • 659
  • 5
  • 19
0

You have to use JSON format for second array.

var reference = ["Name", "Address1", "Address2", "City", "State", "Zip Code", "Formatted Address"];
var toSort = { "Name": "kd","State": "test", "Address1": "tt"};
var sorted={};
for(var i=0;i<reference.length;i++)
{
if(toSort.hasOwnProperty(reference[i]))
  {
    sorted[reference[i]]=toSort[reference[i]];
  }
}
console.log(sorted);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Nivethi Mathan
  • 105
  • 1
  • 8
0

This is a simple algorithm in js without jQuery. orderArray[i] could be called a key and the associative arrays in js are really objects, but that is just semantics.

myOrderArray = ["Name", "Address1", "Address2", "City", "State", "Zip Code", "Formatted Address"];

myUnsortedArray = { Name: "kd", State: "test", Address1: "tt" };

function sortArrayByArray( unsortedArray, orderArray ) {
   let sortedArray = {};
   
   for ( var i = 0; i < orderArray.length; i++ ) {
     if ( orderArray[i] in unsortedArray ) {
        sortedArray[orderArray[i]] = unsortedArray[orderArray[i]];
     }
   }
   
   return sortedArray;

}

mySortedArray = sortArrayByArray( myUnsortedArray, myOrderArray );

console.log( mySortedArray );
JasonB
  • 6,243
  • 2
  • 17
  • 27
0

As I have already commented, second one looks more like an Object, not an array. It has to be initialized as var second = {Name: "kd", State: "test", Address1: "tt"};

Your final output would be in a Array() which is enclosed in an Object called finlObj. Please find the below code. You can optimize the code. But for your understanding, i did in a elaborate way:

 var second = {Name: "kd", State: "test", Address1: "tt"};
     var first = ["Name", "Address1", "Address2", "City", "State", "Zip Code", "Formatted Address"];
     var finlObj ={specList:[]};

     for(var elem in first){
           if(second[first[elem]] !== undefined && second[first[elem]] !== null){


     finlObj.specList[finlObj.specList.length] = second[first[elem]];
     }
     }

     for(var el in finlObj.specList){
     alert(finlObj.specList[el]);

     }
Jayanth
  • 746
  • 6
  • 17
0

This won't work. In JavaScript, by virtue of an array being an object, it will accept non-integer keys as you show here, but neither the runtime or provided methods for working with arrays will allow you any control over their order. This is completely up to the interpreter implementation.

Likewise JavaScript objects are unsortable and, while they usually appear in alphabetical order when displayed in debug tools, this is generally a convenience provided by the browser.

What you need is a Map. You can sort this with the representations you have shown here.

John Halbert
  • 1,320
  • 2
  • 12
  • 23