问答详情
源自:7-7 Swift-函数类型

字符串比较

实在想不出怎么用一条语句既做到按字符串长度排序又可以按照首字母顺序两个同时排序,所以干脆用试试用链式编程,没想到还真可以。。在swift2.0中写的

var strArr=["d","cd","bcd","abcd","abc","ab","a"]


strArr.sort()


func compareTwoString(s1:String , s2:String)->Bool{

    return s1.characters.count<s2.characters.count //|| s1.characters.first<s2.characters.first

    

}


func compareTwoString2(s1:String , s2:String)->Bool{

    return s1.characters.first<s2.characters.first

    

}


strArr.sort(compareTwoString2).sort(compareTwoString)


提问者:慕前端2992628 2015-11-01 23:56

个回答

  • 白云蓝天
    2016-02-17 14:54:57

    嗯,很不错