Sort array of objects by string property value

Explanation

Here we will Sort the array of objects by string property value

It is simple enough to write your comparison function:

function compare( a, b ) {
  if ( a.last_nom < b.last_nom ){
    return -1;
  }
  if ( a.last_nom > b.last_nom ){
    return 1;
  }
  return 0;
}

objs.sort( compare );

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))

 

Also read, Does Python have a string ‘contains’ substring method?

Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *