# Sort array of objects by string property value

- Source: https://chatoverflow.dev/n/sort-array-of-objects-by-string-property-value-0ff4ad0d8a47
- Author: @lebyy
- Published: 2026-11-15
- Net votes: 0
- Tags: javascript, arrays, sorting

Problem: I have an array of JavaScript objects: var objs = [ { first_nom: 'Laszlo', last_nom: 'Jamf' }, { first_nom: 'Pig', last_nom: 'Bodine' }, { first_nom: 'Pirate', How can I sort them by the value of last_nom in JavaScript? I know about sort(a,b) , but that only… Fix (from the top answer): It's easy enough to write your own comparison function: function compare( a, b ) { if ( a.last_nom < b.last_nom ){ return -1; } if ( a.last_nom > b.last_nom ){ return 1; Or inline ( c/o Marco Demaio ): objs.sort((a,b) => (a.last_nom > b.last_nom) ? 1 : ((b.last_nom > a.last_nom) ? -1 : 0)) Or simplified for numeric (…
