Swift排序后输出存在冗余值,如何实现高可读性的格式化展示?
Fixing Redundant Output & Improving Readability for Student Sorting in Swift
Let's work through fixing your code to get clean, properly formatted output after sorting:
First, let's identify the root causes of your issues:
- Directly printing the sorted array with
print(x)outputs Swift's default debug description for theStudentstruct—this is the "redundant useless value" you're seeing. - Your
studfunction is hardcoded to use the globalstudentarray instead of the passedstudsparameter, so it never actually prints the sorted list even when you pass it in.
Here's the corrected, polished code with clean, aligned output:
struct Student { var id: Int = 0 var name: String = "" var course: String = "" var GPA: Float = 0.0 } // Original student dataset let students = [ 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) ] // Updated function to print formatted, aligned student lists func printStudentList(_ studs: [Student]) { // Use string formatting to ensure consistent column alignment print(String(format: "%-12s %-10s %-15s %-8s %s", "Student No.", "ID", "Name", "Course", "GPA")) print("---------------------------------------------------------------") // Loop through the passed array (not global) with enumerated indexes for (index, student) in studs.enumerated() { print(String(format: "%-12d %-10d %-15s %-8s %.2f", index+1, student.id, student.name, student.course, student.GPA)) } } // Sort students by GPA in ascending order let sortedStudentsByGPA = students.sorted { $0.GPA < $1.GPA } // Print both original and sorted lists with clean formatting print("Original Student List:") printStudentList(students) print("\nSorted Student List (By GPA Ascending):") printStudentList(sortedStudentsByGPA)
Key Improvements Made:
- Fixed Parameter Usage: The
printStudentListfunction now uses the passedstudsarray instead of the globalstudentvariable, so it correctly prints whatever list you pass (original or sorted). - Removed Redundant Output: We got rid of
print(x)and instead use our formatted function to display the sorted results. - Clean Column Alignment: Used
String(format:)with width specifiers (like%-12s) to keep columns neatly aligned, even with varying name lengths. - Readable Naming: Renamed variables and functions to be more intuitive (e.g.,
studentsinstead ofstudent,printStudentListinstead ofstud).
Sample Output:
Original Student List: Student No. ID Name Course GPA --------------------------------------------------------------- 1 201520032 Ton Agnis BSITWMA 3.69 2 201620122 Juan Cruz BSCSSE 2.23 3 201723214 Pedro Sy BSITAGD 2.87 4 201418492 Phot xPro BSCPE 3.99 Sorted Student List (By GPA Ascending): Student No. ID Name Course GPA --------------------------------------------------------------- 1 201620122 Juan Cruz BSCSSE 2.23 2 201723214 Pedro Sy BSITAGD 2.87 3 201520032 Ton Agnis BSITWMA 3.69 4 201418492 Phot xPro BSCPE 3.99
内容的提问来源于stack exchange,提问作者Tonzkie
相关产品推荐
相关产品推荐

