gpt4 book ai didi

ios - 按工作日名称swift3对字典数组进行排序

转载 作者:行者123 更新时间:2023-12-01 18:38:23 25 4
gpt4 key购买 nike

我想按工作日名称对根数组进行排序。就像在根数组的第一个索引上应该有星期日、星期一、星期二等。

[{

day = Monday;
timingValues = (
{
closeHour = 23;
closeMin = 55;
day = Monday;
id = 2;
openHour = 00;
openMin = 00;
}
);
},

{

day = Tuesday;

timingValues = (

{
closeHour = 00;
closeMin = 11;
day = Tuesday;
id = 4;
openHour = 12;
openMin = 10;
}
);
}, {

day = Sunday;
timingValues = (
{
closeHour = 23;
closeMin = 07;
day = Sunday;
id = 5;
openHour = 11;
openMin = 07;
}

);

}]

最佳答案

很多力解包,但你可以得到比较的想法,还需要确保字符串是正确的,我这里只是做1行作为引用,它不安全,在实际代码中应该做可选检查。

let dictArray = [["day": "Monday", "timingValues" : 1],
["day" : "Tuesday", "timingValues" : 2],
["day" : "Saturday", "timingValues" : 3],
["day" : "Sunday", "timingValues" : 4]]
let b = dictArray.sorted(by: { DateFormatter().weekdaySymbols.index(of: ($0["day"]! as! String))! < DateFormatter().weekdaySymbols.index(of: ($1["day"]! as! String))! })

结果:
[["day": "Sunday", "timingValues": 4], ["day": "Monday", "timingValues": 1], ["day": "Tuesday", "timingValues": 2], ["day": "Saturday", "timingValues": 3]]

关于ios - 按工作日名称swift3对字典数组进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47525792/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com