【问题标题】:Sort method is only sorting appended strings but not the strings that were initially in arraySort 方法仅对附加的字符串进行排序,而不是对最初在数组中的字符串进行排序
【发布时间】:2019-02-03 19:58:39
【问题描述】:

我试图有一个按钮来保存文本字段中的内容,然后将它们添加到它们的数组中,然后对数组进行排序。但是,无论何时进行排序,我的数组中的前两个字符串都不会排序,但第三个字符串和其他保存的字符串都会排序。

这是数组

//Array holding Names
var names: [String] =
  [

    "Alexander Jones",
    "Tony Stark",
    "cap"

  ]

//Array holding emails
var emails: [String] =
  [

    "alex@fiu.edu",
    "Tony@Stark.com",
    "cap@ca"

  ]

//Index to move through arrays
var index: Int = 0

这里是按钮功能(请忽略它的更新部分,我正在尝试在解决这个问题之前让排序工作

@IBAction func updateButtonPressed(_ sender :UIButton)
  {

    var name = nameField.text
    let email = emailField.text

    print("0 \(names)")
    print("0 \(emails)")

    //if the email or name isn't modified
    //assume user is updating card
    if (name==names[index]) || (email==emails[index])
    {
        //if both textfileds are same as index, update since values will stay the same
        //if one filed is updated at the current index, it will be changed at the index and
        //still match the index of other array
        names[index]=name!
        emails[index]=email!

    }
    else
    {
        //if user puts a new value for both
        //we assume it is a new entry and add it to list
        names.append(name!)
        emails.append(email!)
        print("After Add")
        print("1 \(names)")
        print("1 \(emails)")

        //sort list in alphabetical after each update/save

        names = names.sorted(by: <)
        print("After sorted by name")
        print("2 \(names)")
        print("2 \(emails)")

        //find index new name is sorted too
        //and place new email in same index
        //of emailarray
        let newindex = names.index(of:name!)
        //emails.remove(at: index)
        emails.insert(email!, at: newindex!)
        print("After email insert")
        print("3 \(names)")
        print("3 \(emails)")
    }

  }

【问题讨论】:

  • 为什么不使用包含一个人的姓名和电子邮件的结构而不是两个数组?代码相当繁琐。

标签: arrays swift sorting


【解决方案1】:

您的姓名未按预期排序的主要原因是在字符串排序中,大字符排在第一位

B, a, c

为避免这种行为,您可以使用 lowercased() 小写 String


无论如何,不​​要有两个单独的数组。只有一个您的定制模型。所以,首先使用struct创建您的自定义模型

struct Person {
    var name, email: String
}

然后创建人员数组

var people = [Person(name: "Alexander Jones", email: "alex@fiu.edu"),
              Person(name: "Tony Stark", email: "Tony@Stark.com"), 
              Person(name: "cap", email: "cap@ca")]

然后在按下按钮后追加新的Person 并按所有元素的小写name 排序

@IBAction func updateButtonPressed(_ sender: UIButton) {
    people.append(Person(name: nameField.text!, email: emailField.text!))
    people.sort { $0.name.lowercased() < $1.name.lowercased() }
}

您还可以将didSet 观察者添加到您的数组中,然后每次添加一些新元素或更改数组时,您的数组都会被排序

var people = [Person(name: "Alexander Jones", email: "alex@fiu.edu"),
              Person(name: "Tony Stark", email: "Tony@Stark.com"), 
              Person(name: "cap", email: "cap@ca")] {
    didSet {
        people.sort { $0.name.lowercased() < $1.name.lowercased() }
    }
}
...
@IBAction func updateButtonPressed(_ sender: UIButton) {
    people.append(Person(name: nameField.text!, email: emailField.text!))
}

最后一件事,如果您需要在第一次按下按钮之前对数组进行排序,请不要忘记在控制器的 viewDidLoad 中对其进行排序

【讨论】:

  • 为了完整性:您没有处理名称或邮件已经存在的更新部分。
  • 哇,我想我从来没有注意到这个案例是一个让我发疯的问题,谢谢。再次感谢您提供了一种更易于管理的方法。我对此很陌生,所以对我所遵循的书之外我能做的事情的了解有限。
猜你喜欢
  • 2012-07-05
  • 2016-07-18
  • 1970-01-01
  • 2012-11-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多