简体   繁体   中英

How can I sort an array of arrays (by variable name) using Swift?

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

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?

Try the following approach:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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