title: Vue onclick sort child component in parent component
body: <p>I'm using Vue cli and I have an array of users that I display as a profile card in template that I would like to <strong>sort by name</strong>.
  I'm using Vuex to store data.</p>
  <p>In the child component I have in my template:</p>
  <pre><code>&lt;div class=&quot;outside&quot;  v-for=&quot;(item, index) in arrays&quot; :key=&quot;item.id&quot; v-bind:id=&quot;item.name&quot;&gt;
      &lt;div class=&quot;stay&quot;&gt;  &lt;p class=&quot;n&quot;&gt; {{ item.name }}&lt;/p&gt; &lt;/div&gt;
  </code></pre>
  <p>and I'm getting the Vuex data:</p>
  <pre><code>computed: {
      arrays () {
        return this.$store.state.arrays
      },
  }
  </code></pre>
  <p>and my array contains:</p>
  <pre><code> arrays: [
        { name: 'Peter'},
        { name: 'Beth'},
        { name: 'Daniel'},
      ]
  </code></pre>
  <p>I used a function in the child component:</p>
  <pre><code>computed:{
     sortedArray: function() {
      function compare(a, b) {
        if (a.name &lt; b.name)
          return -1;
        if (a.name &gt; b.name)
          return 1;
        return 0;
      }
  
      return this.arrays.sort(compare);
    }
    },
  </code></pre>
  <p>and changed the template to:</p>
  <pre><code>&lt;div class=&quot;outside&quot;  v-for=&quot;(item, index) in sortedArrays&quot; :key=&quot;item.id&quot; v-bind:id=&quot;item.name&quot;&gt;
  </code></pre>
  <p><strong>But I'd like a way to do this onclick (in the parent), instead of always displaying it in order.</strong></p>
  <p>In my parent component I have:</p>
  <pre><code>&lt;div class=&quot;begin&quot;&gt;
  &lt;div class=&quot;b&quot;&gt;Sort By:
                  
   &lt;select name=&quot;sort&quot; class=&quot;sort&quot; &gt;
    &lt;option value=&quot;name&quot; @click=&quot;sortArrays()&quot;&gt;Name&lt;/option&gt;
   &lt;/select&gt;
   &lt;/div&gt;
  
  &lt;childComponent&gt;&lt;/childComponent&gt;
  
  &lt;/div&gt;
  </code></pre>
  <p>I would like a sortArrays function I can use in the parent to display the user profile card in alphabetical order when I click on sort by name.</p>
  <p>Thanks so much!</p>