title: how to sort an object alphabetically within an array, in React.js
body: <p>I am making an app using React.js and firebase where users can enter the name, and the person's address.</p>
  
  <p>The object in firebase looks like this:</p>
  
  <p><code>L1NSTjqA10Qc85d7Kcc
   - name: John Doe,
   - address: 1 Maple Ave</code></p>
  
  <p>I have printed the name, and address successfully onto the DOM, but i would like to sort it alphabetically, by the name.</p>
  
  <p>So my list would look like this for example:</p>
  
  <pre><code>Jane Doe - 34 Green Street
  John Doe - 1 Maple Ave
  Riley Smith - 3 Happy Street
  </code></pre>
  
  <p>Here is my JSX so far, for when mapping over the array of objects, on the parent component:</p>
  
  <pre><code>&lt;ul&gt;
    {this.state.contacts.map((res)=&gt; &lt;List data={res}/&gt;)}
  &lt;/ul&gt;
  </code></pre>
  
  <p>and just in case, here is my componentDidMount:</p>
  
  <pre><code>componentDidMount() {
    const dbRef = firebase.database().ref();
  
    dbRef.on("value", (firebaseData) =&gt; {
      const itemsArray = [];
      const itemsData = firebaseData.val();
      for (let itemKey in itemsData) {
        itemsArray.push(itemsData[itemKey])
      }
      this.setState({ contacts: itemsArray})
    })
  }
  </code></pre>
  
  <p>I have tried to do the following, from another stack overflow forum, however it did not work:</p>
  
  <pre><code>const myData = [].concat(this.state.contacts)
    .sort((a,b)=&gt; a.name &gt; b.name
    .map((res)=&gt; &lt;List data={res}/&gt;);
  </code></pre>