Sorting an array of objects in javascript by key value does not work : <p>I am trying to sort and array of objects in javascript that represent interfaces of devices. I have found some code here that seems to work for some cases and does not for others.</p>

<p>So here is my code:</p>

<pre><code>compareByName = function(a, b) {
    console.log("sort by name");
    const nameA = a.name.toUpperCase();
    const nameB = b.name.toUpperCase();

    let comparison = 0;
    if (nameA &gt; nameB) {
        comparison = 0;
    } else if (nameA &lt; nameB) {
        comparison = -1;
    }
    return comparison;
}

vm.interfaceData.sort(compareByName);
</code></pre>

<p>It works when i have objects like this:</p>

<pre><code>[
 {
   "name": "eth4",
 },
 {
   "name": "eth3",
 },
 {
   "name": "eth2",
 },
 {
   "name": "eth2",
 }
]

eth0
eth1
eth2
eth3
</code></pre>

<p>But when i have names like this it seems not to work anymore:</p>

<pre><code>[
  {
   "name": "GigabitEthernet0/8",
  },
  {
   "name": "SBB",
  },
  {
   "name": "SR-2",
  },
  {
   "name": "GigabitEthernet0/7",
  },
  {
   "name": "SR-1",
  },
  {
   "name": "GigabitEthernet0/5",
  },
  {
   "name": "GigabitEthernet0/4",
  }
]

GigabitEthernet0/8          
SBB         
SR-2        
GigabitEthernet0/7          
SR-1        
GigabitEthernet0/5          
GigabitEthernet0/4 
</code></pre>
