如何使用swift 3将核心数据值按降序排序,我需要对下面提到的代码进行降序排序的代码,并且我希望将排序数组中的前五个最大数字存储到另一个数组变量中
import UIKit
import Social
import FBSDKLoginKit
import CoreData
class ViewControllerGame: UIViewController {
var seconds = 30
var timer = Timer()
var cur = 0;
var d = 0;
var someValue: Int = 0 {
didSet {
scoreLabel.text = "\(someValue)"
}
}
@IBOutlet weak var tapHere: UIButton!
@IBOutlet weak var label: UILabel!
@IBOutlet weak var scoreLabel: UILabel!
@IBAction func highScoreBtn(_ sender: Any) {
highscoreAlertView(title: "HIGHSCORES", message:"")
}
func highscoreAlertView(title:String, message:String){
let highscorealert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.alert)
highscorealert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: { (action) in
highscorealert.dismiss(animated: true, completion: nil)
}))
self.present(highscorealert, animated: true, completion: nil)
}
@IBAction func tapHere(_ sender: UIButton) {
someValue += 1
d = self.someValue
}
@IBOutlet weak var sliderOutlet: UISlider!
@IBOutlet weak var startOutlet: UIButton!
@IBAction func start(_ sender: AnyObject)
{
timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(ViewControllerGame.counter), userInfo: nil, repeats: true)
startOutlet.isHidden = true
tapHere.isHidden = false
scoreLabel.isHidden = false
}
func counter()
{
if(self.seconds != 0) {
seconds -= 1
label.text = String(format: "%02d", self.seconds % 60 )
}
if (seconds == 0) {
timer.invalidate()
self.label.text = "00"
createAlert(title: "Your Score is", message: "\(self.someValue)")
let appDelegate = (UIApplication.shared.delegate) as! AppDelegate
let context = appDelegate.persistentContainer.viewContext
let score = ScoreList(context:context)
score.newScore = Int32(self.someValue)
appDelegate.saveContext()
do {
let result = try context.fetch(ScoreList.fetchRequest())
let scores = result as! [ScoreList]
for score in scores {
print("Scores: \(score.newScore)")
}
} catch {
print("Error")
}
self.seconds = 30
}
}
@IBAction func shareButton(_ sender: Any) {
let shareToFacebook : SLComposeViewController = SLComposeViewController(forServiceType: SLServiceTypeFacebook)
shareToFacebook.setInitialText("Timer Game score is \(d)")
self.present(shareToFacebook, animated: true, completion: nil)
self.commonthing()
}
func createAlert (title:String, message:String) {
let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "RETRY", style: UIAlertActionStyle.default, handler: { (action) in
alert.dismiss(animated: true, completion: nil)
self.commonthing()
}))
alert.addAction(UIAlertAction(title: "SHARE", style: UIAlertActionStyle.default, handler: { (action) in
alert.dismiss(animated: true, completion: nil)
let shareToFacebook : SLComposeViewController = SLComposeViewController(forServiceType: SLServiceTypeFacebook)
shareToFacebook.setInitialText("Timer Game score is \(self.d)")
self.present(shareToFacebook, animated: true, completion: nil)
self.commonthing()
}))
self.present(alert, animated: true, completion: nil)
}
func commonthing() {
self.seconds = 30
self.label.text = "30"
self.viewDidLoad()
}
override func viewDidLoad() {
super.viewDidLoad()
someValue = 0
self.seconds = 30
tapHere.isHidden = true
scoreLabel.isHidden = true
startOutlet.isHidden = false
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}