热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Firebase中的查询排序顺序是什么?-Firebasequerysortorderinswift?

WhenIloadthefollowingFirebaseDatabasedataintomytableView,thedataissortedinascending

When I load the following Firebase Database data into my tableView, the data is sorted in ascending order by date. How can I order this by descending (show the newest post at the top)?

当我将以下Firebase数据库数据加载到我的tableView中时,数据按日期按升序排序。如何通过降序(在顶部显示最新的帖子)来订购?

Query in Xcode:

在Xcode中查询:

let ref = self.rootRef.child("posts").queryOrderedByChild("date").observeEventType(.ChildAdded, withBlock: { (snapshot) -> Void in

JSON export:

JSON导出:

"posts" : {
    "-KMFYKt7rmfZINetx1hF" : {
      "date" : "07/09/16 12:46 PM",
      "postedBy" : "sJUCytVIWmX7CgmrypqNai8vGBg2",
      "status" : "test"
    },
    "-KMFYZeJmgvmnqZ4OhT_" : {
      "date" : "07/09/16 12:47 PM",
      "postedBy" : "sJUCytVIWmX7CgmrypqNai8vGBg2",
      "status" : "test"
    },

Thanks!!

谢谢!!

EDIT: Below code is the entire solution thanks to Bawpotter

编辑:感谢Bawpotter,代码是完整的解决方案

Updated query:

更新的查询:

let ref = self.rootRef.child("posts").queryOrderedByChild("date").observeEventType(.ChildAdded, withBlock: { (snapshot) -> Void in

    let post = Post.init(key: snapshot.key, date: snapshot.value!["date"] as! String, postedBy: snapshot.value!["postedBy"] as! String, status: snapshot.value!["status"] as! String)

    self.posts.append(post)

    self.tableView.insertRowsAtIndexPaths([NSIndexPath(forRow: self.posts.count-1, inSection: 0)], withRowAnimation: .Automatic)

tableView cellForRowAtIndexPath

tableView cellForRowAtIndexPath

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("PostCell", forIndexPath: indexPath) as! PostCell

        self.posts.sortInPlace({$0.date > $1.date})
        self.tableView.reloadData()

Post.swift:

Post.swift:

import UIKit

class Post {
    var key: String
    var date: String
    var postedBy: String
    var status: String

    init(key: String, date: String, postedBy: String, status: String){
        self.key = key
        self.date = date
        self.postedBy = postedBy
        self.status = status
    }
}

2 个解决方案

#1


10  

When Firebase loads the data into your tableView data source array, call this:

当Firebase将数据加载到tableView数据源数组中时,请调用:

yourDataArray.sortInPlace({$0.date > $1.date})

yourDataArray.sortInPlace({$ 0.date> $ 1.date})

Swift 3 Version:

Swift 3版本:

yourDataArray.sort({$0.date > $1.date})

Swift 4 Version:

Swift 4版本:

yourDataArray.sort(by: {$0.date > $1.date})

#2


1  

While I do recommend doing what was posted and creating a Class and doing it that way, I will give you another way to do sort it.

虽然我建议做的是发布和创建一个类并以这种方式进行,但我会给你另一种方法来对它进行排序。

Since you already have it sorted in Ascending order from Firebase and you know the amount of records, you can do this:

由于您已经从Firebase按升序排序,并且您知道记录的数量,因此您可以执行以下操作:

guard let value = snapshot.children.allObjects as? [FIRDataSnapshot] else {
                    return
}
var valueSorted: [FIRDataSnapshot] = [FIRDataSnapshot]()
var i: Int = value.count
while i > 0 {
    i = i - 1
    valueSorted.append(value[i])
}

推荐阅读
author-avatar
润滑油一_576
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有