如何使用Swift对数组(由变量名)排序?

时间:2021-10-06 05:07:32

(I'm not using Swift 2.0 at the time.)

(我当时没有使用Swift 2.0。)

I have multiple arrays inside a master array. I need them in alphabetical order by variable name, and I can't figure out how to sort anything other than Strings inside an array. These are my arrays:

主数组中有多个数组。我需要它们以变量名的字母顺序来排序,我不知道如何对数组中的字符串进行排序。这些是我的数组:

var onDeck1 = [String]()
var onDeck2 = [String]()
var onDeck3 = [String]()
var onDeck4 = [String]()

My problem is that I keep running into something like this:

我的问题是我经常遇到这样的事情:

var masterArray = [onDeck1, onDeck4, onDeck3, onDeck2]

When I need this:

当我需要:

var masterArray = [onDeck1, onDeck2, onDeck3, onDeck4]

Anybody know what I need to do?

有人知道我需要做什么吗?

1 个解决方案

#1


0  

Try the following approach:

试试下面的方法:

// var masterArray = [ onDeck1, onDeck4, onDeck3,onDeck2]

var masterArray = [ "Babisko", "Kapisko", "Hapisko"];
var sortedArray = masterArray.sorted { $0.localizedCaseInsensitiveCompare($1) == NSComparisonResult.OrderedAscending }

#1


0  

Try the following approach:

试试下面的方法:

// var masterArray = [ onDeck1, onDeck4, onDeck3,onDeck2]

var masterArray = [ "Babisko", "Kapisko", "Hapisko"];
var sortedArray = masterArray.sorted { $0.localizedCaseInsensitiveCompare($1) == NSComparisonResult.OrderedAscending }