如何按子数组首时间对含ÖÖ/ÖS标识的时间二维数组排序?
按子数组首个时间排序二维数组的Swift实现
首先明确时间标识对应关系:
ÖÖ: AM
ÖS: PM
原始时间数组:
let testArray = [["12:15 ÖS", "02:00 ÖS"],["09:00 ÖÖ", "10:00 ÖÖ"],["10:30 ÖÖ", "12:00 ÖS"]]
问题分析
你的代码无法正确解析时间,因为DateFormatter的"hh:mm a"格式默认识别的是AM/PM,而非自定义的ÖÖ/ÖS,导致解析出的Date可能为nil,排序逻辑无法正常工作。另外,直接打印Date对象会输出系统默认的英文格式,需要额外处理才能输出中文时间。
解决方案一:替换标识后用Date解析排序
先将自定义时间标识替换为标准的AM/PM,再解析为Date进行排序,最后可将排序后的Date转换为中文格式的字符串输出。
let testArray = [["12:15 ÖS", "02:00 ÖS"],["09:00 ÖÖ", "10:00 ÖÖ"],["10:30 ÖÖ", "12:00 ÖS"]] // 转换自定义时间标识为标准格式 func convertToStandardTimeString(_ timeStr: String) -> String { return timeStr.replacingOccurrences(of: "ÖÖ", with: "AM") .replacingOccurrences(of: "ÖS", with: "PM") } // 配置用于解析的DateFormatter let parseFormatter = DateFormatter() parseFormatter.dateFormat = "hh:mm a" parseFormatter.locale = Locale(identifier: "en-US") // 转换原始数组,同时保留原始字符串和解析后的Date var convertedArray: [(original: [String], dates: [Date])] = [] for timePair in testArray { let firstStandard = convertToStandardTimeString(timePair[0]) let secondStandard = convertToStandardTimeString(timePair[1]) if let firstDate = parseFormatter.date(from: firstStandard), let secondDate = parseFormatter.date(from: secondStandard) { convertedArray.append((original: timePair, dates: [firstDate, secondDate])) } } // 按子数组首个时间排序 let sortedArray = convertedArray.sorted { $0.dates.first! < $1.dates.first! } // 配置用于输出中文时间的DateFormatter let chineseFormatter = DateFormatter() chineseFormatter.dateFormat = "HH:mm a" chineseFormatter.locale = Locale(identifier: "zh-CN") // 打印排序结果 for item in sortedArray { print("原始时间: \(item.original)") let firstChinese = chineseFormatter.string(from: item.dates[0]) let secondChinese = chineseFormatter.string(from: item.dates[1]) print("中文格式: [\(firstChinese), \(secondChinese)]\n") }
解决方案二:直接计算总分钟数排序(更高效)
无需使用Date,直接将时间转换为一天中的总分钟数,通过数值比较排序,避免时区和格式化问题。
let testArray = [["12:15 ÖS", "02:00 ÖS"],["09:00 ÖÖ", "10:00 ÖÖ"],["10:30 ÖÖ", "12:00 ÖS"]] // 将时间字符串转换为一天中的总分钟数 func timeStringToMinutes(_ timeStr: String) -> Int { let components = timeStr.components(separatedBy: " ") guard components.count == 2 else { return 0 } let timePart = components[0] let period = components[1] let hourMinute = timePart.components(separatedBy: ":") guard hourMinute.count == 2, let hour = Int(hourMinute[0]), let minute = Int(hourMinute[1]) else { return 0 } var totalHours = hour if period == "ÖS" { // PM时段处理 totalHours += hour == 12 ? 0 : 12 } else { // AM时段处理 totalHours = hour == 12 ? 0 : hour } return totalHours * 60 + minute } // 按子数组首个时间的总分钟数排序 let sortedArray = testArray.sorted { timeStringToMinutes($0[0]) < timeStringToMinutes($1[0]) } // 打印排序后的原始数组 print("排序后的数组: \(sortedArray)")
输出结果说明
两种方法排序后的数组顺序一致,正确顺序为:[["09:00 ÖÖ", "10:00 ÖÖ"], ["10:30 ÖÖ", "12:00 ÖS"], ["12:15 ÖS", "02:00 ÖS"]]
使用中文格式输出时,显示效果如下:
原始时间: ["09:00 ÖÖ", "10:00 ÖÖ"] 中文格式: [09:00 上午, 10:00 上午] 原始时间: ["10:30 ÖÖ", "12:00 ÖS"] 中文格式: [10:30 上午, 12:00 下午] 原始时间: ["12:15 ÖS", "02:00 ÖS"] 中文格式: [12:15 下午, 14:00 下午]
内容的提问来源于stack exchange,提问作者Ufuk Köşker
相关产品推荐
相关产品推荐

