title: How to sort a React state array?
body: <p>I'm trying to sort a react state array which contains data from json file. The format of data in json is:</p>
  <pre><code>[
    {
      &quot;name&quot;: &quot;Rob Don&quot;,
      &quot;popularity&quot;: 84,
      &quot;id&quot;: &quot;da6&quot;
    },
    {
      &quot;name&quot;: &quot;Henry Black&quot;,
      &quot;popularity&quot;: 34,
      &quot;id&quot;: &quot;138&quot;
   },
   {
      &quot;name&quot;: &quot;Chris Smith&quot;,
      &quot;popularity&quot;: 56,
      &quot;id&quot;: &quot;39c&quot;
   },...]
  </code></pre>
  <p>The code is below:</p>
  <pre><code>import React, { Component } from 'react';
  import data from '../resources/myData.json';
  
  class HOME extends Component {
  constructor(props) {
      super(props);
      this.state = {  
          person: [...data.slice(0, 5)]
      };
  }
  //SOME MORE CODE
  </code></pre>
  <p>I tried using the below logic but it doesn't work. I don't know why?</p>
  <pre><code>onSortChange = () =&gt; {
      this.state.person.sort((a, b) =&gt; 
          (a.name.toLowerCase() - b.name.toLowerCase())?-1:1
       )
  }
  </code></pre>
  <p>My question is how do I sort <code>this.state.person</code> based on name?</p>