title: Group and Sort Dictionary Data in TableViewController with Swift
body: <p>I have a Dictionary data structure like below and I am trying to group them in my TableViewController such that Group A displays <strong>MyData</strong> that starts with <strong>title</strong> = A and at the same time display <strong>sectionIndexTitlesForTableView</strong> with available letters gotten from Title.</p>
  
  <p>[This is my what I want to achieve]</p>
  
  <p><a href="https://i.stack.imgur.com/t1GL1.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/t1GL1.png" alt="Prototype"></a></p>
  
  <p>I have tried to scrap off all the first letters from the title Element in my Dictionary and save them in a set using the code below but when I run my app, I get results duplicated in my table.</p>
  
  <p>I am quite new to swift and would be glad to be guided on how to achieve this.</p>
  
  <p>Here's my Dictionary Data:</p>
  
  <pre><code>var data: [[String:AnyObject]] =
  
  [
    [
      "id": "1",
      "title": "A Title",
      "alphabet": "A",
      "Detail": "This is a String"
    ],      
    [
      "id": "2",
      "title": "A Title Again",
      "alphabet": "A",
      "Detail": "This is a String"
    ],
    [
      "id": "3",
      "title": "B Title",
      "alphabet": "B",
      "Detail": "This is a String"
    ],
    [
      "id": "4",
      "title": "B Title Again",
      "alphabet": "B",
      "Detail": "This is a String"
    ]
  ]
  </code></pre>
  
  <p>And Here's my attempt:</p>
  
  <pre><code>class Index: UITableViewController {
  
  var MyData = data
  
  var letters = Set&lt;String&gt;()
  
  
  override func viewDidLoad() {
      super.viewDidLoad()
      for element in MyData {
          var title = element["title"] as? String
          let letter = title?.substringToIndex(advance(title!.startIndex, 1))         
          letters.insert(letter!)
      }
  
  
    MyData =   MyData.sort { element1, element2 in
          let title1 = element1["title"] as? String
          let title2 = element2["title"] as? String
  
          return title1 &lt; title2
      }
  
  }
  
  // MARK: - Table view data source
  
  override func numberOfSectionsInTableView(tableView: UITableView) -&gt; Int {
      // #warning Incomplete implementation, return the number of sections
      return letters.count
  }
  
  override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -&gt; Int {
  
          return self.MyData.count
  }
  
  
  override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -&gt; UITableViewCell {
      let cell = self.tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell?
  
          cell!.textLabel?.text = (MyData[indexPath.row]["title"] as! String)
  
      return cell!
  
  }
  </code></pre>