我们如何用快速语言对uitableview数据进行排序(升序和降序)

时间:2022-01-24 04:29:26

i have a question about uitableview.i have a fruits list in uitableview and a filter button in right side navigation bar. how we can sort a fruits list when we click filter button. what should i do in filterList function. below is code.please look it.

我有一个关于uitableview的问题。我在uitableview中有一个水果列表,在右侧导航栏中有一个过滤按钮。当我们点击过滤按钮时,我们如何对水果列表进行排序。我该怎么做filterList函数。下面是code.please看看吧。

/* 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
    }
    }
}

3 个解决方案

#1


18  

Well, just like it sounds - you'd filter the list:

好吧,就像它听起来一样 - 你要过滤列表:

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

If you just want to reverse the items rather than actually sort them (to toggle the order) you can perform a fruitArr.reverse() instead of sorting.

如果您只想反转项目而不是实际排序(切换顺序),您可以执行fruitArr.reverse()而不是排序。

#2


1  

Swift 3+

you can use .sorted() and .lowercased() (with string properties) to sort a-z, by default .sorted and .sort sort by A-z.

您可以使用.sorted()和.lowercased()(使用字符串属性)对a-z进行排序,默认情况下.sorted和.sort按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'

See documentation for .sorted()

请参阅.sorted()的文档

#3


-1  

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

我用来向元数添加和排序元素的最简单方法是在索引0处添加它们:

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

#1


18  

Well, just like it sounds - you'd filter the list:

好吧,就像它听起来一样 - 你要过滤列表:

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

If you just want to reverse the items rather than actually sort them (to toggle the order) you can perform a fruitArr.reverse() instead of sorting.

如果您只想反转项目而不是实际排序(切换顺序),您可以执行fruitArr.reverse()而不是排序。

#2


1  

Swift 3+

you can use .sorted() and .lowercased() (with string properties) to sort a-z, by default .sorted and .sort sort by A-z.

您可以使用.sorted()和.lowercased()(使用字符串属性)对a-z进行排序,默认情况下.sorted和.sort按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'

See documentation for .sorted()

请参阅.sorted()的文档

#3


-1  

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

我用来向元数添加和排序元素的最简单方法是在索引0处添加它们:

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