Sort方法仅对附加的字符串进行排序,而不对最初在数组中的字符串进行排序

时间:2019-02-03 19:58:40

标签: arrays swift sorting

我正在尝试一个按钮,用于在文本字段中保存内容,然后将其添加到其数组中,然后对数组进行排序。但是,无论何时进行排序,数组中的前两个字符串都不会被排序,但是第三个以及其他保存的字符串将被排序。

这是数组

//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)")
    }

  }

1 个答案:

答案 0 :(得分:0)

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

B, a, c

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


无论如何,不​​要有两个单独的数组。仅拥有您的一种定制模型。因此,首先使用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