简体   繁体   English

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

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

(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? 有人知道我需要做什么吗?

Try the following approach: 请尝试以下方法:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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