NSSortDescriptor evaluating ascending numbers (Swift)

前端 未结 3 1147
面向向阳花
面向向阳花 2021-01-18 01:00

App has contentid coming in as a number string from a json file:

let contentid: AnyObject! = jsonFeed[\"contentid\"]

let stream:Dictionary = [
         


        
3条回答
  •  臣服心动
    2021-01-18 01:21

    The values you want to sort are actually strings and not numbers, thus the strange sort order. For Swift there exist an initializer init(key:ascending:selector:) of NSSortDescriptorand thus you can use selector: "localizedStandardCompare:" as described for example at nshipster.com/nssortdescriptor

    The localizedStandardCompare: gives you a Finder like sorting of string values in a way that numbers are sorted naturally as you would sort numbers. So 1,...,9,10,...,99, 100 etc.

提交回复
热议问题