title: How to sort JSON data in ReactJS
body: <p>Can anyone help me with the sorting of json data in ReactJs please? Right now it is not working properly for me. Also if i want to sort as per the title, would it be same? Thanks.</p>
  
  <p>I am trying as below:</p>
  
  <pre><code>componentDidMount() 
  {
          fetch('https://jsonplaceholder.typicode.com/posts').then((res) =&gt; res.json())
              .then((data) =&gt; {
                  data.sort(function (a, b) {
                      return a.userId&gt; b.userId;
                  })
                  data.sort();
                  this.setState({data: data});
  
              });
  
      }
  
      render() {
          return (
              &lt;div&gt;
                  &lt;br/&gt;&lt;br/&gt;
                  &lt;br/&gt;&lt;br/&gt;
  
                  &lt; table className="table"&gt;
  
                      &lt;th&gt;User Id&lt;/th&gt;
                      &lt;th&gt;Name&lt;/th&gt;
                      &lt;th&gt;Address&lt;/th&gt;
                      &lt;th&gt;Action&lt;/th&gt;
                      &lt;tbody&gt;{this.state.data.map(function (item, key) {
                          return (
                              &lt;tr key={key}&gt;
                                  &lt;td&gt;{item.userId}&lt;/td&gt;
                                  &lt;td&gt;{item.id}&lt;/td&gt;
                                  &lt;td&gt;{item.title}&lt;/td&gt;
                                  &lt;td&gt;{item.body}&lt;/td&gt;
                              &lt;/tr&gt;
                          )
  
                      })}&lt;/tbody&gt;
                  &lt;/table&gt;
  
              &lt;/div&gt;
          )
      }
  </code></pre>