score:1

Accepted answer
let locations = [{
    name: 'Fictional Place',
    address: {
      addressLine1: '123 Imaginary Drive',
      line2: '',
      city: 'Philadelphia',
      state: 'PA',
      zip: '12345'
    }
  },
  {
    name: 'California Dreaming',
    address: {
      addressLine1: '456 Somewhere Blvd',
      line2: '',
      city: 'Sacramento',
      state: 'CA',
      zip: '67890'
    }
  }
]

const alphabeticalSort = property => {    
  let sortOrder = 1 // Add your logic for asc/desc here
  , propArr = property.split(".")
  
  return function(a, b) {    
    if (sortOrder === -1) {
      return b[propArr[0]][propArr[1]].localeCompare(a[propArr[0]][propArr[1]]);
    } else {
      return a[propArr[0]][propArr[1]].localeCompare(b[propArr[0]][propArr[1]]);
    }
  };
};

console.log(locations.sort(alphabeticalSort("address.addressLine1")))
console.log(locations.sort(alphabeticalSort("address.state")))

Related Query

More Query from same tag