2017-01-19 3 views
0

Ich hole Daten vom Modell, d. H. Contacts. Dann sortiere ich dieses Modell nach name Eigenschaft. Aber das Ergebnis ist nicht sortiert. Ich verwende folgenden Code Daten zu sortieren:Sortierung in Swift3

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)") 
    } 

Es gibt mir eine Ausgabe wie:

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": []] 

Ich möchte Ausgabe in sortierter Weise wie:

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

Was mache ich falsch? Oder gibt es noch etwas, um es zu sortieren? Vielen Dank!

EDIT: Ich habe versucht, Code folgende richtigen Reihenfolge zu machen:

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

Ok, das gibt mir in der richtigen Reihenfolge ergeben, wie ich will. Aber das Problem ist, ich möchte es verwenden, aber wie ich es nicht weiß. So wie ich habe var arr_Contacts = [String:[Contact]](). Ich möchte sortedArray zu arr_Contacts zuweisen. Wie kann ich das machen?

Wenn ich es gibt Warnung zuweisen, ist:

enter image description here

Es gibt Fehler:

enter image description here

+3

Sie geben die Ergebnisse in ein Wörterbuch ein, das durch den ersten Buchstaben angegeben wird. Wörterbücher sind ungeordnet. – Paulw11

+0

möchten Sie möglicherweise mit einem [NSOrderedDictionary] (https://cocoapods.org/pods/NSOrderedDictionary) – Fonix

+0

untersuchen Ja, wie kann ich es in der richtigen Reihenfolge machen? – Amanpreet

Antwort

1

Wie andere gesagt haben, ein Wörterbuch ist ungeordnete - wenn Sie eine geordnete Darstellung Ihrer Daten benötigen, haben Sie die falsche Datenstruktur gewählt. Ein Array ist viel besser für geordnete Darstellungen geeignet.

Erstellen Sie ein Array von Wörterbüchern oder einfachen Strukturen, die einen Anfangsbuchstaben mit einem Array von Kontakten kombinieren, die mit diesem Buchstaben beginnen.

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

Es funktioniert !! Vielen Dank :) – Amanpreet

1

Versuchen mit diesem Formatierungs zu sortieren.

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 
} 

Ausgang:

("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

Ich habe ** var arr_Contacts = [String: [Kontakt]]() ** und wie kann ich das ** sortedArray ** zu ** arr_Contacts ** zuweisen. – Amanpreet

+0

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

+0

es gibt Fehler. Bitte überprüfen Sie die aktualisierte Frage. – Amanpreet