Generic sort function
From a short review;

ascending should probably be a parameter in the sortFunc
sort (sortKey) should probably be a parameter in the sortFunc
I feel things should be Spartan (1 char) or spelled out

sortFunc -&gt; sortFunction -&gt; sorter?


I would strongly consider the closure concept (example ion proposal)
I would harmonize the sort values with the object fields,or create a map
There is no need to initialize aParam and bParam with null, the default undefined should do
You probably want to add a comment as to why you compare name at the end
I prefer the if/else if approach even when if performs a return, it's one line less and increases readability

This is my counter-proposal;


const sortAscending = true;
const sortDescending = false;

function createSortFunction(key, ascending){

  const keyMap = {
    brand : 'brand.title',
    weight: 'metadata.weight',
    price: 'price.unit_amount',
    style: 'metadata.style',
    arrival: 'created'
  }
  
  key = keyMap[key]? keyMap[key] : 'brand.title';
  
  return (function sortFunction(a, b){
  
    //Minor magic, derive the property from a the dot notation string
    key.split('.').forEach(part=&gt;{a = a[part], b = b[part];}); 
    
    // Sort by property, ascending or descending
    if (a &lt; b) {
      return ascending ? -1 : 1
    }else if (b &gt; a) {
      return ascending ? 1 : -1
    }
  
    // Sort by name if the properties are same
    if (a.name &lt; b.name) {
      return -1
    } else if (a.name &gt; b.name) {
      return 1
    }
    //a and b are equal
    return 0;
  });
}


products = [
 { name : 'Bob',   metadata: { weight: 160, style: 'sage'}, price: { unit_amount: 145}, created : '19750421', brand : { title: 'Sir' } },
 { name : 'Skeet', metadata: { weight: 130, style: 'ninja'}, price: { unit_amount: 160}, created : '20010611', brand : { title: 'Bro' } }
]

products.sort(createSortFunction('weight', sortAscending));
console.log(products);