title: Comparing Original Array of Dom Elements with Sort Array of Dom Elements and check if they are different
body: <p>I have a structure of dynamic DOM elements that I'm continuously evaluating with a for loop and a setInterval. While in that interval I sort the array and then change the dom with a JQuery Sort function in order to order my DOM structure on the fly.</p>
  <p>The thing is I only want to change the DOM if the sort function is actually changing the order of elements (usually this happens when a new DOM element comes into the structure dynamically), if the sort function runs again over an already sortered array I don't want to do anything with the dom.</p>
  <p>I have found A LOT of ways to compare if Arrays are equal but my case is special since i don't really care about the arrays elements / content but I really need to check if those elements are not in the same order so i can change the dom.</p>
  <pre><code>    setInterval(function () {       
      //Incoming Chat Structure Variables
      var $incomingChatsTable, $incomingChatsRows, $incomingChatTags
      $incomingChatsTable= $(&quot;div[__jx__id*='incoming_list__list_content_container']&quot;)
      $incomingChatsRowsContainer=$(&quot;div[__jx__id='___$_194__visitor_list__incoming_list__list__content']&quot;)
      $incomingChatsRows = $(&quot;div[class*='renderers_Incoming']&quot;)
      $incomingChatTags = $( &quot;div[__jx__id*='incoming_list__list__content'] div[class*='renderers_Incoming'] .visitorlist_tags .jx_ui_html_span:last-child .tag&quot;).toArray()        
      $popupTrigger = $(&quot;div[jx\\:list\\:rowid]&quot;) 
      //Assigning Priorities Function
      function assignPriority(arrayParent) {
          for ( var i=0; i &lt;= arrayParent.length; i++) {
              var actualRow = $(arrayParent[i])
              var minutesInQueue = $(actualRow).find('.time_cell').text().substr(0,2)
              var priorityVal = parseInt(minutesInQueue)
              var actualRowTags = $( actualRow ).find('.tag').toArray()
              $(actualRow).addClass('priorityRow')               
              for (var k=0; k &lt;= actualRowTags.length; k++) {
                  let normalHolderTag = $(actualRowTags[k]).text().toLowerCase()
                  if (normalHolderTag === 'vip') {
                      priorityVal += 30
                      $(actualRow).attr('data-priority', priorityVal)                    
                  } else if ( normalHolderTag === 'rg' ) {
                      priorityVal += 20
                      $(actualRow).attr('data-priority', priorityVal)
                  } else if ( normalHolderTag === 'accountclosure' ) {
                      priorityVal += 10
                      $(actualRow).attr('data-priority', priorityVal)                    
                  } else {
                      $(actualRow).attr('data-priority', priorityVal)   
                  } 
                  //$(actualRow).find('.numbers_cell').text(priorityVal)
              }
          }     
      }   
      //Sorting Incoming Chats By Priority
      function orderByPriority(container) {
          myArray = container.find('.priorityRow')
          var changed = false
          myArray.sort(function(a,b){
              contentA = parseInt( $(a).data('priority') )
              contentB = parseInt( $(b).data('priority') )
              if ( (contentB - contentA) * ( myArray.indexOf(b) - myArray.indexOf(a) ) &gt; 0 ){
                  changed = true
              }                     
              return (contentB - contentA)                             
          })
          if(changed) {
              $(sortedArray).prependTo( container )
          } else{
              console.log('No need To Sort')
          }
      }
      setInterval(function () {
          assignPriority( $incomingChatsRows )
          orderByPriority( $incomingChatsRowsContainer )
      }, 500)
  }, 1000) 
  </code></pre>
  <p><strong>UPDATE:</strong></p>
  <p>myArray is now a global variable, I have added an IF statement evaluating the sort function result and multiplying that result by the a, b indexes. If that's greater than 0 then i change the value of my &quot;changed&quot; variable to &quot;true&quot; BUT I'm getting a &quot;<strong>myArray.indexOf is not a function</strong>&quot; error.</p>