title: Nested Sort in Struct Array in Swift 5
body: <p>Here its my model looks like</p>
  <pre><code>struct ContactModel{
      var alphabet: String
      var users: [UserModel]
  }
  
  struct UserModel{
      var firstname: String
      var lastname: String
  }
  </code></pre>
  <p>Using <code>sort</code> alphabetically sorting working fine here Is the code:</p>
  <pre><code>contactArray.sort{ $0.alphabet &lt; $1.alphabet }
  </code></pre>
  <p>Now, I want to sort ascending or descending order on bases of <code>UserModel</code> <code>firstname</code>
  I try with nested sorting but giving me different errors like</p>
  <pre><code>Comparable and Binary operator '&lt;' cannot be applied to two '[UserModel]' operands
  </code></pre>
  <p>I search on StackOverflow regarding this but haven't find nested sorting and operands error solution.</p>
  <p>I tried this way</p>
  <pre><code>let sortedStudents = contactArray.sorted { (lhs: ContactModel, rhs: ContactModel) -&gt; Bool in
                  let users1 = lhs.users.sorted(by: {
                      $0.firstname.lowercased() &lt; $1.firstname.lowercased()
                  })
                  let users2 = rhs.users.sorted(by: {
                      $0.firstname.lowercased() &lt; $1.firstname.lowercased()
                  })
                  
                  return users1 &lt; users2
              }
  </code></pre>
  <p>But its not working. The only problem here that I am not able to do nested sort here want to sort firstname of UserModel and the We have <code>ContactModel</code> Array</p>
  <pre><code>var contactArray = [ContactModel](){
          didSet{
              
              contactArray.sort{ $0.alphabet &lt; $1.alphabet } //Simple Sort working fine.
              
              let sortedStudents = contactArray.sorted { (lhs: ContactModel, rhs: ContactModel) -&gt; Bool in
                  let users1 = lhs.users.sorted(by: {
                      $0.firstname.lowercased() &lt; $1.firstname.lowercased()
                  })
                  let users2 = rhs.users.sorted(by: {
                      $0.firstname.lowercased() &lt; $1.firstname.lowercased()
                  })
                  
                  return users1 &lt; users2
              }
              
              self.tableView.reloadData()
          }
      }
  </code></pre>
  <p>Array Contains this</p>
  <pre><code> func getAllContacts() -&gt; [ContactModel]{
          return [
              ContactModel(alphabet: &quot;B&quot;, users: [
                  UserModel(firstname: &quot;Bhaswar&quot;, lastname: &quot;Patel&quot;),
                  UserModel(firstname: &quot;Bunty&quot;, lastname: &quot;Patel&quot;)
              ]),
              ContactModel(alphabet: &quot;N&quot;, users: [
                  UserModel(firstname: &quot;Nilesh&quot;, lastname: &quot;Darji&quot;)
              ]),
              ContactModel(alphabet: &quot;Y&quot;, users: [
                  UserModel(firstname: &quot;Yogesh&quot;, lastname: &quot;Patel&quot;),
                  UserModel(firstname: &quot;Yash&quot;, lastname: &quot;Patel&quot;)
              ])
          ]
      }
  </code></pre>
  <p>Results:</p>
  <p>Alphabetic section sorting working fine need to sort users now using firstname for this <code>Yash</code> is comes first then <code>Yogesh</code></p>
  <p><a href="https://i.stack.imgur.com/n3GUO.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/n3GUO.png" alt="" /></a></p>
  <p>Any suggestion appreciated. Thanks!</p>