在Xcode(Swift)中,我有一个数组,它被初始化为100个空项:
var persons = [String](count:100, repeatedValue: "")
使用一些函数,我可以将内容添加到数组中的某些位置,从0开始。
所以例如,在某个时刻我的数组可能是这样的:
["Bert", "Daniel", "Claire", "Aaron", "", "", ... ""]
这段代码用于将数组按字母顺序排序,其中空项由点号表示:
persons = persons.sorted {$0.localizedCaseInsensitiveCompare($1) == NSComparisonResult.OrderedAscending }
这会给我返回这样的一组数组:
["", "", ... , "Aaron", "Bert", "Claire", "Daniel"]
我想对我的数组进行字母顺序排序,但不需要将空元素排在前面。我需要得到如下的数组:
["Aaron", "Bert", "Claire", "Daniel", "", "", ... , ""]
就我个人而言,我不想要含有空项的数组,但是我发现如果我不像声明一个100项的数组,我就无法向其中添加值(这个数组肯定不会填满100项)。
有人能帮帮我吗?