2

This link tells you what your sorting function has to look like, but I just can't make a Swift function look like that.

Here's my call:

packetsArray.sortUsingFunction(comparePacketDates, context: nil)

Here's my function, and I've tried a zillion other variations of it:

static func comparePacketDates(packet1 : AnyObject, packet2: AnyObject, context: UnsafeMutablePointer<Void>) -> Int {

And I've tried using a c function instead, this and a few other variations:

NSInteger comparePacketDates(id packet1, id packet2, void* dummy);

It's always "cannot invoke with argument list ..." saying the function isn't what's expected as the first argument.

Does anyone have example code of a Swift function that would be accepted as the first argument of the Swift sortUsingFunction? Or even a c function that would be accepted as the first argument of the Swift SortUsingFunction?

2
  • I think the body of your swift comparePacketDates function is missing. That might be helpful in diagnosing the problem. Also, you might consider just casting the NSMutableArray to a Swift array and using Swift's sorting mechanisms. See here: stackoverflow.com/questions/25837539/… Commented Nov 4, 2015 at 3:52
  • The body of my comparePacketDates doesn't matter, because it's not getting that far. It won't accept the way I've declared it as the first argument in sortUsingFunction. Also if I did caste it to a Swift Array does Swift's sort work for an array of custom objects? Or does it just sort arrays of strings and numbers? Commented Nov 4, 2015 at 14:38

2 Answers 2

4

Use sortUsingComparator. It's simpler than sortUsingFunction. Here's the syntax:

a.sortUsingComparator {
    let packet1 = $0 as! Packet
    let packet2 = $1 as! Packet
    if packet1.timestamp < packet2.timestamp {
        return .OrderedAscending
    }
    if packet1.timestamp == packet2.timestamp {
        return .OrderedSame
    }
    return .OrderedDescending
}
Sign up to request clarification or add additional context in comments.

Comments

0

Here is an example for your reference that I wrote:

var ma = NSMutableArray()

ma.addObject("Hi")
ma.addObject("Anil")

func comp(first: AnyObject, second:AnyObject,  context: UnsafeMutablePointer<Void>) -> Int {
  let f:String  = first as! String
  let s:String = second as! String

  if f > s {
    return 1
  }
  else if ( f == s ) {
    return 0
  }

  return -1

}

ma.sortUsingFunction(comp, context:UnsafeMutablePointer<Void>(bitPattern: 0))

HTH, as a reference to you. Note: I am in Xcode 7.1 Swift 2.1.

2 Comments

This did not work using what I have: XCode 6.4 / Swift 1.2. Same old "cannot invoke". Maybe it's time I upgraded.
I'll bet your solution works in Swift 2.1 but I cannot test it for a while as I was going to upgrade in 2 weeks. I don't normally like the "do it a different way" answers, but rob mayor's solution is working for me so I have to accept his answer. Thx a lot though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.