title: How to sort an array of object depending on value of a subfield?
body: <p>I have an array of objects called posts with a nested field answer. I have different conditions depending on which I need to sort my array.</p>
  <p>Example array</p>
  <pre><code>let posts = [
    {
                          &quot;_id&quot;: &quot;610b806ec6386ffa276b6d1c&quot;,
                          &quot;post&quot;: &quot;CDat the difference between Equity and News channel?&quot;,
                          &quot;answers&quot;: [
                              {
                                  &quot;_id&quot;: &quot;613724604dd4b6f39b344b4c&quot;,
                                  &quot;type&quot;: &quot;text&quot;,
                                  
                              },
                              {
                                  &quot;_id&quot;: &quot;6113c826a64da80fe48ab543&quot;,
                                  &quot;type&quot;: &quot;video&quot;,
                                  
                              },
                              {
                                  &quot;_id&quot;: &quot;6113c740a64da80fe48ab534&quot;,
                                  &quot;type&quot;: &quot;audio&quot;,
                                  
                              },
                              {
                                  &quot;_id&quot;: &quot;611135cf7497d70b6cc2c5e0&quot;,
                                  &quot;type&quot;: &quot;video&quot;,
                                 
                              }
                          ]
                      },
                      {
                          &quot;_id&quot;: &quot;611e14329ff0c343540ae10e&quot;,
                          &quot;post&quot;: &quot;How forex affects investment 6&quot;,
                          &quot;answers&quot;: [
                              {
                                  &quot;_id&quot;: &quot;61371b3a9bf14a39207fff8a&quot;,
                                  &quot;type&quot;: &quot;video&quot;,
                                  
                              },
                              {
                                  &quot;_id&quot;: &quot;613719369507fd12f93e3c62&quot;,
                                  &quot;type&quot;: &quot;text&quot;,
                                  
                              },
                              {
                                  &quot;_id&quot;: &quot;6135f28e29aeae3de45fc8c2&quot;,
                                  &quot;type&quot;: &quot;text&quot;,
                                  
                              },
                              {
                                  &quot;_id&quot;: &quot;6135f07c831da33c28fc1cf6&quot;,
                                  &quot;type&quot;: &quot;audio&quot;,
                                  
                              },
                              {
                                  &quot;_id&quot;: &quot;6135eb2d51a8830698d65cf3&quot;,
                                  &quot;type&quot;: &quot;text&quot;,
                                 
                              },
                             
                          ]
                      }
  
  ]
  </code></pre>
  <p>What I need to do is...I want to sort all the answers with type &quot;video&quot; to start and after that type &quot;audio&quot; and then type &quot;text&quot; answers like so</p>
  <pre><code>
  let posts = [
    {
                          &quot;_id&quot;: &quot;610b806ec6386ffa276b6d1c&quot;,
                          &quot;post&quot;: &quot;CDat the difference between Equity and News channel?&quot;,
                          &quot;answers&quot;: [
                              {
                                  &quot;_id&quot;: &quot;613724604dd4b6f39b344b4c&quot;,
                                  &quot;type&quot;: &quot;video&quot;,
                                  
                              },
                              {
                                  &quot;_id&quot;: &quot;6113c826a64da80fe48ab543&quot;,
                                  &quot;type&quot;: &quot;video&quot;,
                                  
                              },
                              {
                                  &quot;_id&quot;: &quot;6113c740a64da80fe48ab534&quot;,
                                  &quot;type&quot;: &quot;audio&quot;,
                                  
                              },
                              {
                                  &quot;_id&quot;: &quot;611135cf7497d70b6cc2c5e0&quot;,
                                  &quot;type&quot;: &quot;text&quot;,
                                 
                              }
                          ]
                      },
                      {
                          &quot;_id&quot;: &quot;611e14329ff0c343540ae10e&quot;,
                          &quot;post&quot;: &quot;How forex affects investment 6&quot;,
                          &quot;answers&quot;: [
                              {
                                  &quot;_id&quot;: &quot;61371b3a9bf14a39207fff8a&quot;,
                                  &quot;type&quot;: &quot;video&quot;,
                                  
                              },
                              {
                                  &quot;_id&quot;: &quot;613719369507fd12f93e3c62&quot;,
                                  &quot;type&quot;: &quot;audio&quot;,
                                  
                              },
                              {
                                  &quot;_id&quot;: &quot;6135f28e29aeae3de45fc8c2&quot;,
                                  &quot;type&quot;: &quot;text&quot;,
                                  
                              },
                              {
                                  &quot;_id&quot;: &quot;6135f07c831da33c28fc1cf6&quot;,
                                  &quot;type&quot;: &quot;text&quot;,
                                  
                              },
                              {
                                  &quot;_id&quot;: &quot;6135eb2d51a8830698d65cf3&quot;,
                                  &quot;type&quot;: &quot;text&quot;,
                                 
                              },
                             
                          ]
                      }
  
  ]
  
  </code></pre>
  <p>Any help would be appreciated. Thank you</p>