You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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 the Student struct—this is the "redundant useless value" you're seeing.
  • Your stud function is hardcoded to use the global student array instead of the passed studs parameter, 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:

  1. Fixed Parameter Usage: The printStudentList function now uses the passed studs array instead of the global student variable, so it correctly prints whatever list you pass (original or sorted).
  2. Removed Redundant Output: We got rid of print(x) and instead use our formatted function to display the sorted results.
  3. Clean Column Alignment: Used String(format:) with width specifiers (like %-12s) to keep columns neatly aligned, even with varying name lengths.
  4. Readable Naming: Renamed variables and functions to be more intuitive (e.g., students instead of student, printStudentList instead of stud).

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 03:56:44