简体   繁体   English

Swift 按枚举声明的顺序对枚举数组进行排序

[英]Swift sort an array of enums by their enum declared order

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我想按照枚举中声明的顺序对 arrayOfEducationOptions 进行排序以获取 [.gcse, .aLevel, .masters]

Make it conform to CaseIterable protocol and use the static allCases property:使其符合CaseIterable协议并使用静态allCases属性:

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

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:看起来 Vadim 的想法是正确的,但是如果您想对 EducationOptions 的任意列表进行排序,您可以使用如下代码:

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 . (它至少具有O(n²)时间性能,甚至可能更差( O(n²•log n) )。对于较大的枚举,您需要重写它以创建一个结构数组,其中包含来自示例数组的索引枚举,然后排序

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可能有更好的方法来设置数组中的顺序值,同时也具有字符串原始值,但不是我的头顶

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM