Sort Array of Objects by specific key value

This question already has an answer here:

  • Sort array of objects by string property value in JavaScript 33 answers
  • Sorting in JavaScript: Shouldn't returning a boolean be enough for a comparison function? 2 answers

  • You can use localeCompare method which will returns a number indicating whether a reference string comes before or after or is the same as the given string in sort order.

    var sorted = array.sort((a, b) => {
      return a.subreddit.localeCompare(b.subreddit)
    });
    

    DEMO

    链接地址: http://www.djcxy.com/p/19338.html

    上一篇: 按数组索引排序

    下一篇: 按特定键值排序对象数组