gpt4 book ai didi

swift - Dart 中的列表排序与 Swift 中的排序列表不同

转载 作者:行者123 更新时间:2023-11-30 10:52:50 24 4
gpt4 key购买 nike

我已经在 iOS 版 Swift 中成功实现了排序算法。(参见下面的代码)。

现在,我想在 Dart 中为 Flutter 实现相同的算法。

而且我必须意识到我的试验(见下文)与 Swift 代码的效果不同。为什么???

谁能解释一下 Swift 的 sorted 函数和 Dart 的 sort 函数之间的区别吗?为什么我的下面的代码片段在 Swift 和 Dart 中没有做同样的事情???

这是 Swift 代码:

return stationItems.sorted {
let nameA = $0.name!
.replacingOccurrences(of: ",", with: "", options: .literal, range: nil)
.replacingOccurrences(of: "ä", with: "a", options: .literal, range: nil)
.replacingOccurrences(of: "ö", with: "o", options: .literal, range: nil)
.replacingOccurrences(of: "ü", with: "u", options: .literal, range: nil)
.lowercased()
let nameB = $1.name!
.replacingOccurrences(of: ",", with: "", options: .literal, range: nil)
.replacingOccurrences(of: "ä", with: "a", options: .literal, range: nil)
.replacingOccurrences(of: "ö", with: "o", options: .literal, range: nil)
.replacingOccurrences(of: "ü", with: "u", options: .literal, range: nil)
.lowercased()
let searchTermy = searchTerm
.replacingOccurrences(of: ",", with: "", options: .literal, range: nil)
.replacingOccurrences(of: "ä", with: "a", options: .literal, range: nil)
.replacingOccurrences(of: "ö", with: "o", options: .literal, range: nil)
.replacingOccurrences(of: "ü", with: "u", options: .literal, range: nil)
.lowercased()

if nameA == searchTermy && nameB != searchTermy {
return true
} else if nameA.hasPrefix(searchTermy) && !nameB.hasPrefix(searchTermy) {
return true
} else if nameA.contains(searchTermy) && !nameB.contains(searchTermy) {
return true
} else {
let n = searchTermy.count
for i in 0..<searchTermy.count {
if nameA.hasPrefix(String(searchTermy[..<(n-i)])) && !nameB.hasPrefix(String(searchTermy[..<(n-i)])) {
return true
} else {
return false
}
}
return false
}
}

这是 Dart 代码:

return stationList.sort((a, b) {
var nameA = a.stopName
.replaceAll(RegExp(','), '')
.replaceAll(RegExp('ä'), 'a')
.replaceAll(RegExp('ö'), 'o')
.replaceAll(RegExp('ü'), 'u')
.toLowerCase();
var nameB = b.stopName
.replaceAll(RegExp(','), '')
.replaceAll(RegExp('ä'), 'a')
.replaceAll(RegExp('ö'), 'o')
.replaceAll(RegExp('ü'), 'u')
.toLowerCase();
var searchTermy = stationName
.replaceAll(RegExp(','), '')
.replaceAll(RegExp('ä'), 'a')
.replaceAll(RegExp('ö'), 'o')
.replaceAll(RegExp('ü'), 'u')
.toLowerCase();
if ((nameA == searchTermy) && (nameB != searchTermy)) {
return 1;
} else if (nameA.startsWith(searchTermy) && !nameB.startsWith(searchTermy)) {
return 1;
} else if (nameA.contains(searchTermy) && !nameB.contains(searchTermy)) {
return 1;
} else {
var n = searchTermy.length;
for (int i = 0; i < searchTermy.length; i++) {
if (nameA.startsWith(searchTermy.substring(0, (n - i))) && !nameB.startsWith(searchTermy.substring(0, (n - i)))) {
return 1;
} else {
return 0;
}
}
return 0;
}
});

最佳答案

true/false 与这里的 0/1 不同。

Dart 排序检查 3 种不同的东西。

比较AB ,我们有 3 种可能性:

  • 回调返回0 。在这种情况下AB相等
  • 回调返回一个数字 > 0 。这意味着A位于之后 B
  • 回调返回一个数字 < 0 。在这种情况下A位于之前 B .

关于swift - Dart 中的列表排序与 Swift 中的排序列表不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54278030/

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