Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 在swift中对数组中的对象进行自定义排序_Arrays_Sorting_Object_Types_Swift2 - Fatal编程技术网

Arrays 在swift中对数组中的对象进行自定义排序

Arrays 在swift中对数组中的对象进行自定义排序,arrays,sorting,object,types,swift2,Arrays,Sorting,Object,Types,Swift2,需要按字母顺序对对象(类/数据类型)进行排序 未排序数组:: [main.Apple, main.Zoo, main.IceCream, main.Apple, main.IceCream] 排序数组::按字母顺序键入@前面 [main.Apple, main.Apple, main.IceCream, main.IceCream, main.Zoo] 这是我迄今为止找到的解决方案之一: 使用sortInPlace函数和dynamicType检查类名,例如:-String(firstData

需要按字母顺序对对象(类/数据类型)进行排序

未排序数组::

[main.Apple, main.Zoo, main.IceCream, main.Apple, main.IceCream]
排序数组::按字母顺序键入@前面

[main.Apple, main.Apple, main.IceCream, main.IceCream, main.Zoo]

这是我迄今为止找到的解决方案之一:

  • 使用
    sortInPlace
    函数和
    dynamicType
    检查类名,例如:-
    String(firstDataType.dynamicType)。组件由String(“.”)分隔。last这将检索对象的类名

    unsortedArray.sortInPlace({(firstDataType,secondDataType) in
    return String(firstDataType.dynamicType).componentsSeparatedByString(".").last!
        .localizedStandardCompare(String(secondDataType.dynamicType).componentsSeparatedByString(".").last!) == NSComparisonResult.OrderedAscending
    
    })