0

I'm looking for sort my dropdown list into ascending order.

let set = NSSet(array: arrTemp as! [Any])
let arrNewPredicated = set.allObjects as NSArray
arrAllArrayCode = NSMutableArray (array: arrNewPredicated)

I have 17 elements that looks like this

[0]=(NSTaggedPointerString*)"AAA"
[1]=(NSTaggedPointerString*)"CCC"
[2]=(NSTaggedPointerString*)"BBB"

I would like to order them by ascending order.

Leo Dabus
  • 229,809
  • 59
  • 489
  • 571
jo7332
  • 9
  • 2
  • 4
    You should drop the NS prefix when coding in Swift. Use Set instead of NSSet, Array instead of NSArray/NSMutableArray and String instead of NSString/NSTaggedPointerString – Leo Dabus Apr 24 '19 at 14:12

3 Answers3

1

I supposed arrTemp is an array of Strings so:

first of all you're using Swift so as LeoDabus suggests drop NS prefix. Than build your set:

let arrayOfStrings = ["beta", "omega", "alpha", "teta"]
let set = Set<String>(arrayOfStrings)

then just sort it:

let sorted = set.sorted()
Mahdi Moqadasi
  • 2,029
  • 4
  • 26
  • 52
Alastar
  • 1,284
  • 1
  • 8
  • 14
0

If you use an array of sortable elements:

let arrTemp = ["AAA", "CCC", "BBB"]
let set = Set(arrTemp)
let arrNewPredicated = set.sorted { (str1, str2) -> Bool in
    return str1 < str2
    // or use
    // return str2 < str1
}
Agisight
  • 1,778
  • 1
  • 14
  • 15
0

I found the correct line of code for my issues. If that can help someone..

let set = NSSet(array: arrTemp as! [Any])
let arrNewPredicated = set.allObjects as NSArray
let sortarray = arrNewPredicated.sortedArray(using: [NSSortDescriptor(key: "", ascending: true)])  as NSArray
jo7332
  • 9
  • 2