Int的Shuffle结构 [英] Shuffle struct by Int

查看:116
本文介绍了Int的Shuffle结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何通过可变优先级 改组结构并将其显示在TableView中?



所以现在我的结构中有 20 个文档,但是以后我的结构中将有 100 + 个文档。



5或7或10个文档的优先级为 从10到1,其他文档的 priority 0。我需要在tableView的顶部位置显示5或7或10个文档。优先级为 0的其他文档必须以随机顺序位于前5、7或10个文档之后。



I 。 e。应根据优先级放置前5、7或10个文档,如果文档的优先级为 10,则应为第一个文档,第二个文档为 priority 9应该位于优先级为 priority $$code> 10的文档后面,依此类推。优先级为1的文档应以此类推。



此代码可帮助我从 firestore 获取文档:

  fileprivate func watchQuery(){
MBProgressHUD.showAdded(to:self.view,animation:true)
保护让查询=查询其他{返回}
let time = DispatchTime.now()+ 0.5
listener = query.addSnapshotListener {如果让快照=快照{
DispatchQueue .main.asyncAfter(最后期限:时间){
var photoModels = snapshot.documents.map {(document)->
中的摄影师,如果让photoModel = Photographer(dictionary:document.data(),id:document.documentID){
return photoModel
}否则{
fatalError(致命错误 )
}
}
self.photographers = photoModels
//需要使用随机播放
self.document =快照文件
self.tableView.reloadData ()
MBProgressHUD.hide(用于:self.view,动画:true)
}
}
}
}


解决方案

您可以做的是




  • 首先通过降低优先级对文档进行排序。

  • 然后使用优先级为零的文档对数组的一部分进行混洗。



示例:

  var sorted = documents.sorted(作者:{$ 0.priorit & $ 1.priority})
如果让idx = sorted.firstIndex(其中:{$ 0.priority == 0}){
sorted [idx ...]。shuffle()
}

另一种方法是改组整个数组,然后通过使用以下命令降低优先级来进行稳定排序 如何快速稳定地对数组排序的想法?

  let sorted = document.shuffled()。enumerated()
.sorted(by:{($ 0.element .priority,$ 0.offset)>($$。element.priority,$ 1.offset)})
.map {$ 0.element}

这将按降序对条目进行排序,并随机洗改具有相同优先级的 all 条目,不仅是具有零优先级的条目。



备注: Swift标准库中的sort方法在Swift 5中恰好是稳定的,因此最后一种方法可以简化为

  le t sorted = document.shuffled()
.sorted(by:{$ 0.priority> $ 1.priority})

但是,这不能保证,请比较在Swift论坛中sort()在Swift 5中稳定吗?。 p>

How I can shuffle my struct by variable priority and display in TableView?

So now I have 20 documents in my struct, but later I will have 100+ documents in my struct.

5 or 7 or 10 documents will have priority from 10 to 1, other documents have priority 0. Me need display 5 or 7 or 10 documents on top position in tableView. Other documents which have priority 0 must be located after firsts 5 or 7 or 10 documents in random order.

I. e. the firsts 5 or 7 or 10 documents should be placed depending on the priority, if a document has priority 10, it should be the first one, the next one which has priority 9 should be behind the document with priority 10 and so on to the document with priority 1. Other documents due be randomly in order.

This code which help me get documents from firestore:

fileprivate func observeQuery() {
    MBProgressHUD.showAdded(to: self.view, animated: true)
    guard let query = query else { return }
    let time = DispatchTime.now() + 0.5
    listener = query.addSnapshotListener { [unowned self] (snapshot, error) in
        if let snapshot = snapshot {
            DispatchQueue.main.asyncAfter(deadline: time) {
                var photoModels = snapshot.documents.map { (document) -> Photographer in
                    if let photoModel = Photographer(dictionary: document.data(), id: document.documentID) {
                        return photoModel
                    } else {
                        fatalError("Fatal error")
                    }
                }
                self.photographers = photoModels
                // this need use shuffle
                self.document = snapshot.documents
                self.tableView.reloadData()
                MBProgressHUD.hide(for: self.view, animated: true)
            }
        }
    }
}

解决方案

What you could do is

  • Sort the documents by decreasing priority first.
  • Then shuffle the part of the array with documents with zero priority.

Example:

var sorted = documents.sorted(by: { $0.priority > $1.priority } )
if let idx = sorted.firstIndex(where: { $0.priority == 0 }) {
    sorted[idx...].shuffle()
}

An alternative is to shuffle the complete array and then do a "stable sort" by decreasing priority, using the ideas from How to stable sort an array in swift?:

let sorted = documents.shuffled().enumerated()
    .sorted(by: { ($0.element.priority, $0.offset) > ($1.element.priority, $1.offset) })
    .map { $0.element }

This would sort the entries by decreasing order, and randomly shuffle all entries with identical priority, not only the entries with zero priority.

Remark: The sort methods from the Swift standard library happens to be stable in Swift 5, so that the last approach could be simplified to

let sorted = documents.shuffled()
    .sorted(by: { $0.priority > $1.priority } )

However, that is not guaranteed, compare Is sort() stable in Swift 5? in the Swift forum.

这篇关于Int的Shuffle结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆