How can we sort (ascending and descending) uitableview data in fast language

I have a question that uitableview.i has a list of fruits in uitableview and a filter button in the right side navigation bar. how can we sort the list of fruits when we click the filter button. what should i do in the filterList function. Below is the code.please code.

/* Fruit.swift */

import Foundation

class Fruit {
    var fruitName = ""

    init(fruitName: String) { self.fruitName = fruitName }

}


class FruitsListViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

    @IBOutlet var fruitList: UITableView!

    @IBOutlet var filterItem: UIBarButtonItem!

    var fruitArr:[Fruit] = Fruit

    override func viewDidLoad() {
        super.viewDidLoad()

        var barButton = UIBarButtonItem(title: "Filter", style: .Plain, target: self, action:"filterList") 
        self.navigationItem.rightBarButtonItem = barButton

        var fruits1 = Fruit(fruitName: "Apple" as String) 
        var fruits2 = Fruit(fruitName: "Mango" as String) 
        var fruits3 = Fruit(fruitName: "Banana" as String) 
        var fruits4 = Fruit(fruitName: "Orange" as String)

        fruitArr.append(fruits1)
        fruitArr.append(fruits2) 
        fruitArr.append(fruits3) 
        fruitArr.append(fruits4)
        }

    func filterList() {/* ? */}

    override func didReceiveMemoryWarning() {
          super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated.
     }

        func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int {       
         return fruitArr.count
         }

        func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
          let cell:ShowFruitCustomCellTableViewCell = tableView.dequeueReusableCellWithIdentifier('FruitCell') as ShowFruitCustomCellTableViewCell
          let fruit = fruitArr[indexPath.row] cell.setCell(fruit.fruitName) return cell
    }
    }
}
+5
source share
4 answers

Well, no matter how it sounds - you would filter the list:

func filterList() { // should probably be called sort and not filter
    fruitArr.sorted() { $0.fruitName > $1.fruitName } // sort the fruit by name
    fruitList.reloadData(); // notify the table view the data has changed
}

If you just want to flip the elements rather than sort them (to reorder), you can do it fruitArr.reverse()instead of sorting.

+19
source

Swift 3+

.sorted() .lowercased() ( ) a-z .sorted .sort sort by A-z.

    //Prints Names A-z (0-9 then A-Z then a-z)
    fruitArr.sorted() { $0.fruitName > $1.fruitName }
    //prints  '000', 'Watermelon', 'apple'

    //Prints Name in alphabetical order.
    fruitArr.sorted() { $0.fruitName.lowercased() > $1.fruitName.lowercased() }
    //prints '000', 'apple', 'Watermelon'

. . sorted()

+1

This is how I make tableView fall, not rise. I use segue to load and transfer data to Tableview.

 // pass some data to the tableViewController in descending order
    override func prepare(for segue: UIStoryboardSegue, sender: Any?)
    {
        if segue.destination is TableViewController
        {
            let vc = segue.destination as? TableViewController
            vc?.passedData   = pointEntryArray.reversed()
            vc?.passedData01 = dataCenterArray.reversed()
        }
    }

NTN

0
source

The easiest way I use to add and sort an element into an array is to add them to index 0:

fruitArr.insert(fruits1, at: 0)
fruitArr.insert(fruits2, at: 0) 
fruitArr.insert(fruits3, at: 0)
fruitArr.insert(fruits4, at: 0)
-3
source

All Articles