Sorting a String array and ignoring case

前端 未结 5 1814
眼角桃花
眼角桃花 2020-12-29 05:09

The code below works for sorting an array of strings if they are all lowercase or all uppercase but I want to ignore case when I sort. How could I do this? The following is

5条回答
  •  感动是毒
    2020-12-29 05:17

    This is the method that should be used and is intended for this purpose:

    public func caseInsensitiveCompare(aString: String) -> NSComparisonResult
    

    In your case:

    resultListArray.sort({ $0.fileName.caseInsensitiveCompare($1.fileName) == NSComparisonResult.OrderedAscending })
    

提交回复
热议问题