2015-03-28 32 views
5

In questo momento (28 marzo 2015): Se voglio usare un ordinamento stabile in swift, devo usare NSArray e sortWithOptions o scrivere il mio ordinamento stabile in Swift come insertion sort? Vedo nello Apple Swift docs che sorted non è stabile.Ordinamento Swift Stable?

Example of stable sort in NSMutableArray:

[array sortWithOptions:NSSortStable usingComparator:^NSComparisonResult(id obj1, id obj2) { 
    return [obj1 compare:obj2]; 
}]; 

Mi sto perdendo un'altra opzione che è disponibile a me in Swift tranne usando Objective-C o scrivere il mio tipo?

+0

sì .. puoi anche scrivere un wrapper rapido che chiama la funzione obiettivo-c - se non ce n'è ancora uno ancora .. – Michael

+0

ovviamente! Ho avuto una scoreggia cerebrale. Fai una risposta e io verde segno di spunta, @ Michael. :) – finneycanhelp

+2

ok ........... :) – Michael

risposta

0

Come @ Michael ha detto, si può "scrivere un rapido-wrapper che chiama la funzione obiettivo-c"

0

Oltre al vostro approccio involucro Swift è possibile utilizzare la funzione C mergesort:

var array2 = ["mango", "apple", "pear", "apple", "orange", "banana"] 
mergesort(&array2, array2.count, MemoryLayout<String>.size, { 
    let a = $0.unsafelyUnwrapped.load(as:String.self) 
    let b = $1.unsafelyUnwrapped.load(as:String.self) 
    if a == b { 
     return 0 
    } 
    else if a < b { 
     return -1 } 
    else { 
     return 1 
    } 
}) 
print(array2) // ["apple", "apple", "banana", "mango", "orange", "pear"] 

o scrivere un approccio Swift puro che rende le cose più generico: (. Questo è il mio approccio umile e semplice, senza dubbio altri possono ottimizzare)

var array = ["mango", "apple", "pear", "banana", "orange", "apple", "banana"] 

enum SortType { 
    case Ascending 
    case Descending 
} 

struct SortObject<T> { 
    let value:T 
    let startPosition:Int 
    var sortedPosition:Int? 
} 

func swiftStableSort<T:Comparable>(array:inout [T], sortType:SortType = .Ascending) { 

    var sortObjectArray = array.enumerated().map{SortObject<T>(value:$0.element, startPosition:$0.offset, sortedPosition:nil)} 

    for s in sortObjectArray { 
     var offset = 0 
     for x in array[0..<s.startPosition] { 
      if s.value < x { 
       offset += sortType == .Ascending ? -1 : 0 
      } 
      else if s.value > x { 
       offset += sortType == .Ascending ? 0 : -1 
      } 
     } 

     for x in array[s.startPosition+1..<array.endIndex] { 
      if s.value > x { 
       offset += sortType == .Ascending ? 1 : 0 
      } 
      else if s.value < x { 
       offset += sortType == .Ascending ? 0 : 1 
      } 
     } 
     sortObjectArray[s.startPosition].sortedPosition = offset + s.startPosition 
    } 

    for s in sortObjectArray { 
     if let sInd = s.sortedPosition { 
      array[sInd] = s.value 
     } 
    } 

} 

swiftStableSort(array: &array, sortType:.Ascending) // ["apple", "apple", "banana", "banana", "mango", "orange", "pear"] 
swiftStableSort(array: &array, sortType:.Descending) // ["pear", "orange", "mango", "banana", "banana", "apple", "apple"] 

Problemi correlati