Swift按枚举宣布的命令排序一系列枚举

发布于 2025-01-30 04:10:06 字数 400 浏览 2 评论 0原文

我如何对声明的顺序排序我的枚举数组?

enum EducationOptions: String {
    case gcse = "GCSE"
    case aLevel = "A Level"
    case bachelors = "Bachelors"
    case masters = "Masters"
    case doctorate = "Doctorate"
    case other = "Other"
}

var arrayOfEducationOptions: [EducationOptions] = [.masters, .gcse, .aLevel]

我想按枚举中声明的顺序排序阵列deducationoptions以获取[.gcse,.alevel,.masters]

How can I sort my array of enums to be sorted by the declared order?

enum EducationOptions: String {
    case gcse = "GCSE"
    case aLevel = "A Level"
    case bachelors = "Bachelors"
    case masters = "Masters"
    case doctorate = "Doctorate"
    case other = "Other"
}

var arrayOfEducationOptions: [EducationOptions] = [.masters, .gcse, .aLevel]

I want to sort arrayOfEducationOptions to get [.gcse, .aLevel, .masters] as per the order declared in the enum

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

游魂 2025-02-06 04:10:06

Vadim似乎有正确的想法,但是如果您想对教育的任意列表进行分类,则可以使用这样的代码:

enum EducationOptions: String, CaseIterable {
    case gcse = "GCSE"
    case aLevel = "A Level"
    case bachelors = "Bachelors"
    case masters = "Masters"
    case doctorate = "Doctorate"
    case other = "Other"
}

let allCases = EducationOptions.allCases

var arrayOfEducationOptions: [EducationOptions] = [.masters, .gcse, .aLevel]
    .sorted {allCases.firstIndex(of: $0)! <  allCases.firstIndex(of: $1)! }

arrayOfEducationOptions.forEach { print($0) }

请注意,该代码是一个天真的实现,并且随着案例数量的增长而扩展很差。 (它至少具有o(n²)时间性能,甚至可能更糟(o(n²•log n))。对于要重写的较大枚举它创建一个结构数组,其中包括来自枚举样本阵列的索引,然后对进行排序。

It seems like Vadim has the right idea, but if you want to sort an arbitrary list of EducationOptions, you could use code like this:

enum EducationOptions: String, CaseIterable {
    case gcse = "GCSE"
    case aLevel = "A Level"
    case bachelors = "Bachelors"
    case masters = "Masters"
    case doctorate = "Doctorate"
    case other = "Other"
}

let allCases = EducationOptions.allCases

var arrayOfEducationOptions: [EducationOptions] = [.masters, .gcse, .aLevel]
    .sorted {allCases.firstIndex(of: $0)! <  allCases.firstIndex(of: $1)! }

arrayOfEducationOptions.forEach { print($0) }

Note that that code is a naive implementation, and would scale very poorly as the number of cases grows. (It would have at least O(n²) time performance, and probably even worse (O(n²•log n)). For larger enums you'd want to rewrite it to create an array of structs that included indexes from the sample array of enums, and then sort that.

黯然 2025-02-06 04:10:06

使其符合caseIterable协议,并使用静态allccases属性:

enum EducationOptions: String, CaseIterable {
    case gcse = "GCSE"
    case aLevel = "A Level"
    case bachelors = "Bachelors"
    case masters = "Masters"
    case doctorate = "Doctorate"
    case other = "Other"
}

let arrayOfEducationOptions = EducationOptions.allCases

Make it conform to CaseIterable protocol and use the static allCases property:

enum EducationOptions: String, CaseIterable {
    case gcse = "GCSE"
    case aLevel = "A Level"
    case bachelors = "Bachelors"
    case masters = "Masters"
    case doctorate = "Doctorate"
    case other = "Other"
}

let arrayOfEducationOptions = EducationOptions.allCases
故事还在继续 2025-02-06 04:10:06

在枚举中添加可比的协议?

enum EducationOptions: String {
   case gcse = "GCSE"
   case aLevel = "A Level"
   case bachelors = "Bachelors"
   case masters = "Masters"
   case doctorate = "Doctorate"
   case other = "Other"

   var order: Int {
      switch self {
      case .gcse: return 1
      case .aLevel: return 2
      case .bachelors: return 3
      case .masters: return 4
      case .doctorate: return 5
      case .other: return 6
   }
}

extention EquctionOptions: Comparable { 


   static func < (lhs: EducationOptions, rhs: EducationOptions) -> Bool {
      lhs.order < rhs.order
   }
}

然后,您可以对数组进行排序。

let array = [.masters, .gcse, .aLevel]
let sorted = array.sorted(by: { $0 < $1 })

在具有字符串原始值时,可能有更好的方法来设置数组中的订单值,但不是我的头顶

Add a comparable protocol to the enum?

enum EducationOptions: String {
   case gcse = "GCSE"
   case aLevel = "A Level"
   case bachelors = "Bachelors"
   case masters = "Masters"
   case doctorate = "Doctorate"
   case other = "Other"

   var order: Int {
      switch self {
      case .gcse: return 1
      case .aLevel: return 2
      case .bachelors: return 3
      case .masters: return 4
      case .doctorate: return 5
      case .other: return 6
   }
}

extention EquctionOptions: Comparable { 


   static func < (lhs: EducationOptions, rhs: EducationOptions) -> Bool {
      lhs.order < rhs.order
   }
}

then you can just sort the array.

let array = [.masters, .gcse, .aLevel]
let sorted = array.sorted(by: { $0 < $1 })

there may be a better way to set the order values in the array while having the String raw value as well, but not of the top of my head

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文