如何在SWIFT中创建类实例的排序数组?

时间:2019-06-14 14:50:58

标签: arrays swift class sorting

我创建了一个学生班和该班的五个实例。如何创建此类实例的排序数组?

class Student {
    var name: String
    var surname: String
    var yearOfBorn: Int
    var mark: Double

    init(name: String, surname: String, yearOfBorn: Int, mark: Double) {
        self.name = name
        self.surname = surname
        self.yearOfBorn = yearOfBorn
        self.mark = mark
    }

    func dataBaseOfStudents() {
        print("Name: \(name), Surname: \(surname), Birthyear: \(yearOfBorn), Mark: \(mark)")
    }
}

let studentOne = Student(name: "Pavel", surname: "Max", yearOfBorn: 2108, mark: 7.5)
studentOne.dataBaseOfStudents()

let studentTwo = Student(name: "Mike", surname: "Wilson", yearOfBorn: 2109, mark: 10.4)
let studentThree = Student(name: "Stiven", surname: "Rodgers", yearOfBorn: 2100, mark: 15.4)
let studentFour = Student(name: "Susan", surname: "Sarandon", yearOfBorn: 2099, mark: 8.7)
let studentFive = Student(name: "Kate", surname: "McGragor", yearOfBorn: 2117, mark: 12.5)

let arrayOfStudents = [studentOne, studentTwo, studentThree, studentFour, studentFive]

它应该得到一个排序数组。最后一个属性(.mark)。 并在控制台中显示结果

0 个答案:

没有答案