2016-09-24 1 views
0

このコードは、このエラーを生成します。その他のスウィフト3:ソート機能付きソート配列:「ソート(コンパレータ:)」があいまいな使用の見誤り

Ambiguous use of 'sort(comparator:)'

chars.sort { 
    let s0 = $0 as? String 
    let s1 = $1 as? String 
    if array.index(of: s0) == array.index(of: s1) { 
     ComparisonResult.orderedSame 
    } else if array.index(of: s0) < array.index(of: s1) { 
     ComparisonResult.orderedDescending 
    } else { 
     ComparisonResult.orderedAscending 
    } 
} 

SOポストはの引数をキャスト提案しましたsortは文字列に対して機能しますが、エラーは継続します。なぜ誰かが説明できますか?

+0

どのように書くことができます

let orders = ["a", "b", "c", "e", "f"] 

別の配列に受注に要素を使用して、それをソートしたい場合'chars'は定義されていますか? 'array'とは何ですか? –

+0

@appzYourLifeは修正されました! 'chars'が' Any'ではなく定義された配列であることを確認している回答を投稿してください。 – Crashalot

+1

私はここでの問題についてまだよく分かりません。 'chars'と' array'がどのように定義されているか教えてください。 –

答えて

1

あなたはこの

let chars: [Any] = ["g", "c", "a"] 

ような配列を持っているとあなたは単に

let sorted = chars 
    .flatMap { $0 as? String } 
    .sorted { orders.index(of:$0) ?? Int.max < orders.index(of: $1) ?? Int.max } 

// print(sorted) 
// ["a", "c", "g"] 
関連する問題