# Remove all but one entity when sorting a group of entities on a tableview cell (Swift5)

**URL:** <https://forums.kodeco.com/t/remove-all-but-one-entity-when-sorting-a-group-of-entities-on-a-tableview-cell-swift5/71956>\
**Category:** Swift\
**Created:** [May 25, 2019, 12:52pm UTC](https://forums.kodeco.com/t/remove-all-but-one-entity-when-sorting-a-group-of-entities-on-a-tableview-cell-swift5/71956 "2019-05-25T12:52:11Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![timswift](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/timswift/32/110937_2.png) [@timswift](https://forums.kodeco.com/u/timswift)\
**Post date:** [May 25, 2019, 12:52pm UTC](https://forums.kodeco.com/t/remove-all-but-one-entity-when-sorting-a-group-of-entities-on-a-tableview-cell-swift5/71956/1 "2019-05-25T12:52:12Z")

</div>

When my code loads 5 entities are displayed on each tableview cell. When I hit a button it sorts the entity but the other 4 entities are still displayed. I have added a photo below to show you exactly what I am looking for. When the objc is pressed it sorts but all of the entities are displayed and i only want 1.  
@objc func l1UPBTN(sender: UIButton){  
let appD = UIApplication.shared.delegate as! AppDelegate

```
        let context = appD.persistentContainer.viewContext

        let fetchRequest = NSFetchRequest<NSManagedObject>(entityName: "Data")
        fetchRequest.sortDescriptors = [NSSortDescriptor(key: "Name", ascending: true)]

        do {
            itemName = try context.fetch(fetchRequest)

            self.theScores.reloadData()
        }catch {
            print("Ashley Tisdale")
        }

           }

         func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let title = itemName[indexPath.row]
let cell = theScores.dequeueReusableCell(withIdentifier: "MyCell", for : indexPath)
cell.textLabel!.numberOfLines = 2
cell.selectionStyle = .default
let attr1 = title.value(forKey: "atBATS") as? String

let attr3 = title.value(forKey: "age") as? String
let attr4 = title.value(forKey: "weight") as? String

let attr2 = title.value(forKey: "hits") as? String
let attr5 = title.value(forKey: "positon") as? String

let text = [attr1," Weight :",attr3,"\n","Height : ",attr2," Age : ",attr4," Position :",attr5].flatMap { $0 }.reduce("", +)
cell.textLabel?.text = "\(text)"

cell.textLabel?.textAlignment = .center

cell.layoutMargins = UIEdgeInsets.zero

cell.preservesSuperviewLayoutMargins = false
cell.separatorInset = UIEdgeInsets.zero
cell.layoutMargins = UIEdgeInsets.zero

return cell

                  }

```

![10%20AM](https://files.chunter.kodeco.com/original/3X/f/6/f652991a2936a167140498cd4d65faca865ea7a1.png)  
LINK TO STACKOVERFLOW QUESTION

> <https://stackoverflow.com/questions/56249432/remove-all-but-one-entity-when-sorting-a-group-of-entities-on-a-tableview-cell>

---

<div class="post-metadata">

**Author:** ![shogunkaramazov](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/shogunkaramazov/32/108826_2.png) [@shogunkaramazov](https://forums.kodeco.com/u/shogunkaramazov)\
**Post date:** [July 9, 2019, 3:12am UTC](https://forums.kodeco.com/t/remove-all-but-one-entity-when-sorting-a-group-of-entities-on-a-tableview-cell-swift5/71956/2 "2019-07-09T03:12:52Z")

</div>

@timswift Do you still have issues with this?

---

<div class="post-metadata">

**Author:** ![system](https://s3.amazonaws.com/cdn.raywenderlich.com/community/forum-assets/noun-game-ghost-878668.png) [@system](https://forums.kodeco.com/u/system)\
**Post date:** [November 8, 2019, 5:03am UTC](https://forums.kodeco.com/t/remove-all-but-one-entity-when-sorting-a-group-of-entities-on-a-tableview-cell-swift5/71956/3 "2019-11-08T05:03:34Z")

</div>

This topic was automatically closed after 166 days. New replies are no longer allowed.
