我的代码的输出都很好,已经排序了,但问题是,它包含一些我不需要的垃圾值,我将提供它的示例输出。
这是我的代码:
struct Student {
var id: Int = 0;
var name: String = String();
var course: String = String();
var GPA: Float = 0.0;
}
let student = [
Student(id: 201520032, name: "Ton Agnis", course: "BSITWMA", GPA: 3.69),
Student(id: 201620122, name: "Juan Cruz", course: "BSCSSE", GPA: 2.23),
Student(id: 201723214, name: "Pedro Sy", course: "BSITAGD", GPA: 2.87),
Student(id: 201418492, name: "Phot xPro", course: "BSCPE", GPA: 3.99)
]
func stud(get studs:[Student]){
print("Student No.\t\tID\t\tName\t\t\tCourse\t\tGPA")
for i in 0...studs.count - 1{
print("Student \(i+1) \t \(student[i].id)\t\(student[i].name)\t\t\(student[i].course)\t\t\(student[i].GPA)")
}
}
let x = student.sorted{ $0.GPA < $1.GPA }
stud(get: student)
print(x)
Here is the Output of the Given Code
如您所见,输出显示了一些不需要的值。
我想要显示的是给定值的可读性更好的排序。
谢谢!
最佳答案
如果您使自定义类符合 CustomStringConvertible 协议(protocol)(添加一个计算变量,description
,类型为 String
),那么当您打印其中一个对象时,它显示格式很好。
您可以使用带制表符的打印语句格式作为起点。
关于swift - 如何使用排序正确显示可读性强的输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48383559/