2017-01-19 7 views
0

モデルからデータを取得しています。つまり、Contactsです。それから私はこのモデルをnameプロパティごとにソートします。しかし、結果はソートされた方法ではありません。私は、データをソートするには、次のコードを使用しています:私が間違っているのは何Swift3でソートする

result......["A": [], "B": [<App.Contact: 0x7c6703e0>], "C": [], "D": []] 

result......["M": [], "K": [<App.Contact: 0x7c6703e0>], "E": [], "U": [], "Y": [], "H": [<App.Contact: 0x7c6701a0>], "X": [], "A": [<App.Contact: 0x7c670120>, <App.Contact: 0x7c670440>], "D": [<App.Contact: 0x7c6700b0>, <App.Contact: 0x7c670160>], "I": [], "R": [], "G": [], "P": [], "O": [], "L": [], "W": [], "C": [], "V": [], "J": [<App.Contact: 0x7c66f990>], "Q": [], "T": [], "B": [], "N": [], "Z": [], "S": [], "F": []] 

は、私のようなソートされた形で出力したい:

func filterArrayAlphabatically(contacts : [Contact]) 
    { 

     let alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".characters.map({ String($0)}) 

     let all_Contacts = contacts.sorted { $0.name < $1.name } //Sort model data as per name 

     var result = [String:[Contact]]() 

     for letter in alphabet 
     { 
      result[letter] = [] 

      for cntct in all_Contacts 
      { 
       let ctc : Contact! = cntct 
       let name : String! = ctc.name.capitalized 

       if name.hasPrefix(letter) 
       { 
        result[letter]?.append(cntct) 
       } 
      } 
     } 

     print("result......\(result)") 
    } 

それは私のような出力を提供しますか?またはそれをソートする他の人がいますか?ありがとう!

EDIT:私は正しい順序を作るために、次のコードを試してみました:

let sortedArray = result.sorted 
    { 
     (struc1, struc2) -> Bool in 
     return struc1.key < struc2.key 
    } 

[OK]を、これは私が好きな私は、正しい順序で結果ができます。しかし、問題は、私はそれを使用したいですが、どのように私は知らない。私はvar arr_Contacts = [String:[Contact]]()を持っています。私はsortedArrayarr_Contactsを割り当てたいと思います。どうやってやるの?

私はそれがある警告を与える割り当てる:

enter image description here

+3

結果を最初の文字をキーにして辞書に入れます。辞書は順序付けられていません。 – Paulw11

+0

[NSOrderedDictionary](https://cocoapods.org/pods/NSOrderedDictionary) – Fonix

+0

を使用して調べることができますはい、どうすれば正しい順序で作成できますか? – Amanpreet

答えて

1

他の人が言ったように、辞書は順不同です - あなたはあなたのデータの順序付けられた表現が必要な場合、あなたは間違ったデータ構造を選択しました。配列は、順序付けされた表現にはるかに適しています。

開始文字とその文字で始まる連絡先の配列を組み合わせた辞書または単純な構造体の配列を作成します。

func filterArrayAlphabatically(contacts inputContacts: [Contact]) -> [[String: [Contact]]] 
{ 
    //Using this will remove any contacts which have a name not beginning with these letters, is that intended? 
    let alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".characters.map({ String($0) }) 

    // For strings use a localized compare, it does sensible things with accents etc. 
    let allContacts = inputContacts.sorted { $0.name.localizedCompare($1.name) == .orderedAscending } 

    //Now we're using an array of dictionaries, each of which will have a single key value pair [letter: [Array of contacts]] 
    var result = [[String: [Contact]]]() 

    for letter in alphabet 
    { 
     result.append([letter: contacts(beginningWith: letter, from: allContacts)]) 
    } 

    return result 
} 

func contacts(beginningWith prefix: String, from contactList: [Contact]) -> [Contact] { 
    //you could use a filter here for brevity, e.g. 
    //let contacts = contactList.filter { $0.name.capitalized.hasPrefix(prefix) } 
    //this is short enough to reasonably be used at the call site and not within a function 

    var contacts = [Contact]() 
    for contact in contactList { 
     if contact.name.capitalized.hasPrefix(prefix) { 
      contacts.append(contact) 
     } 
    } 
    return contacts 
} 

let contacts = [Contact(name: "Bob"), Contact(name: "Kate"), Contact(name: "Darling")] 
let filteredArray = filterArrayAlphabatically(contacts: contacts) 
print(filteredArray) 
//[["A": []], ["B": [Contact(name: "Bob")]], ["C": []], ["D": [Contact(name: "Darling")]], ["E": []], ["F": []], ["G": []], ["H": []], ["I": []], ["J": []], ["K": [Contact(name: "Kate")]], ["L": []], ["M": []], ["N": []], ["O": []], ["P": []], ["Q": []], ["R": []], ["S": []], ["T": []], ["U": []], ["V": []], ["W": []], ["X": []], ["Y": []], ["Z": []]] 
+0

その作業!!ありがとうございました :) – Amanpreet

1

てみてください、この整形でソートする:

enter image description here

それはエラーになります。

class Contact { 
    var name: String = "" 
    var lastName: String = "" 

    public init (name: String, lastName: String){ 
     self.name = name 
     self.lastName = lastName 
    } 
} 

let contact1 = Contact(name: "V", lastName: "W") 
let contact2 = Contact(name: "G", lastName: "W") 
let contact3 = Contact(name: "A", lastName: "C") 
let contact4 = Contact(name: "P", lastName: "W") 
let contact5 = Contact(name: "F", lastName: "W") 
let contact6 = Contact(name: "A", lastName: "W") 
let contact7 = Contact(name: "A", lastName: "W") 
var contacts: [Contact] 

contacts = [contact1, contact2, contact3, contact4, contact5, contact6, contact7] 

let sortedContact = contacts.sorted{(struct1, struct2) -> Bool in 
    return struct1.name < struct2.name 
} 
//At this point our contact are sorted, now add to dictionnary 

var arr_Contacts = [String:[Contact]]() 
let alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".characters.map({ String($0) }) 

for letter in alphabet { 
    arr_Contacts[String(letter)] = [] 
    let matches = sortedContact.filter({$0.name.characters.first == letter.characters.first}) 
    if !matches.isEmpty { 
     for contact in matches { 
      arr_Contacts[letter]?.append(contact) 
     } 
    } 
} 
//Dic are unordered data, so need to use an Array 
let sortedDic = Array(arr_Contacts).sorted{ (s1, s2) in 
    return s1.key < s2.key 
} 

出力

("A", [Contact, Contact, Contact])("B", [])("C", [])("D", [])("E", [])("F", [Contact])("G", [Contact])("H", [])("I", [])("J", [])("K", [])("L", [])("M", [])("N", [])("O", [])("P", [Contact])("Q", [])("R", [])("S", [])("T", [])("U", [])("V", [Contact])("W", [])("X", [])("Y", [])("Z", []) 
+0

私は** var arr_Contacts = [String:[Contact]](**)を** ** sort_Array **を** arr_Contacts **にどのように割り当てることができますか? – Amanpreet

+0

@Amanpreet yourDic = ["your_key_value":sortedArray] – Makaille

+0

エラーが発生します。更新された質問を確認してください。 – Amanpreet