1

I have an array that looks like this:

var persons = [Person]()

And a person contains this:

id : Int
name : String
age : Int
gender : String

In my persons list I have multiple persons, but how do I sort this list on unique names?

I found this let unique = Array(Set(originals)) but it does not seem to work on a list of objects, I want to keep all the information of the object.

If my list contains these rows:

Adam, Adam, John, John, Michael, Nick, Tony

I only want to show:

Adam, John, Michael, Nick Tony

And I still want to be able to get Adams age for example.

Any ideas?

Edit
Person declaration

import Foundation

public class Person : NSObject {
    var id = ""
    var name = ""
    var age = 0
    var gender = ""
}

func ==(lhs: Person, rhs: Person) -> Bool {
    return lhs.name == rhs.name
}
user5855868
  • 97
  • 2
  • 15
  • You need to make your class Person Equatable and then you can use this extension http://stackoverflow.com/questions/25738817/does-there-exist-within-swifts-api-an-easy-way-to-remove-duplicate-elements-fro/34712330?s=1|0.1796#34712330 – Leo Dabus Sep 05 '16 at 04:24
  • `func ==(lhs: Person, rhs: Person) -> Bool { return lhs.id == rhs.id }` – Leo Dabus Sep 05 '16 at 04:30
  • @LeoDabus, I have done that now but I´m not sure how to filter the list now. – user5855868 Sep 05 '16 at 04:30
  • Just use `yourArray.orderedSetValue` – Leo Dabus Sep 05 '16 at 04:31
  • What does *sorting on unique names* mean? Does it mean taking out duplicate records and then sorting on name field? – Ozgur Vatansever Sep 05 '16 at 04:37
  • @ozgur, remove all duplicate names from the list – user5855868 Sep 05 '16 at 04:40
  • @user5855868 please give a minimal, complete, and verifiable example about what kind of result you want your code to produce. – Ozgur Vatansever Sep 05 '16 at 04:43
  • @LeoDabus, no not yet. the `orderedSetValue`do I need an extension for that? – user5855868 Sep 05 '16 at 04:44
  • I have posted the link http://stackoverflow.com/questions/25738817/does-there-exist-within-swifts-api-an-easy-way-to-remove-duplicate-elements-fro/34712330?s=1%7C0.1796%2334712330 – Leo Dabus Sep 05 '16 at 04:45
  • `extension Array where Element: Equatable { var orderedSetValue: Array { return reduce([]){ $0.contains($1) ? $0 : $0 + [$1] } } }` – Leo Dabus Sep 05 '16 at 04:48
  • Yes missed the link, now I have added the extension and added the func == to my Person class, but it´s still the same it does not remove duplicates. The list I get from the database `s` does contain 50 records and `let persons = s.orderedSetValue` does also return 50 records. – user5855868 Sep 05 '16 at 04:54
  • @user5855868 I need to see your Person declaration – Leo Dabus Sep 05 '16 at 04:55
  • @LeoDabus check update – user5855868 Sep 05 '16 at 05:00
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/122634/discussion-between-user5855868-and-leo-dabus). – user5855868 Sep 05 '16 at 05:01

1 Answers1

2

You need to add this extension to return an ordered set from your people array.

extension Collection where Element: Hashable {
    var orderedSet: [Element] {
        var set: Set<Element> = []
        return reduce(into: []){ set.insert($1).inserted ? $0.append($1) : ()  }
    }
}

You will also have to make your Person class Equatable:

func ==(lhs: Person, rhs: Person) -> Bool {
    return lhs.id == rhs.id
} 

class Person: Equatable { 
    let id: String 
    let name: String 
    init(id: String, name: String) { 
        self.id = id 
        self.name = name 
    } 
}

Testing

let person1 = Person(id: "1", name: "Adam") 
let person2 = Person(id: "1", name: "Adam") 
let person3 = Person(id: "2", name: "John") 
let person4 = Person(id: "2", name: "John") 
let person5 = Person(id: "3", name: "Michael") 
let person6 = Person(id: "4", name: "Nick") 
let person7 = Person(id: "5", name: "Tony") 
let people = [person1,person2,person3,person4,person5,person6,person7] 
people.orderedSet //[{id "1", name "Adam"}, {id "2", name "John"}, {id "3", name "Michael"}, {id "4", name "Nick"}, {id "5", name "Tony"}]
Leo Dabus
  • 229,809
  • 59
  • 489
  • 571