0

I have 3 arrays

createdAt = [someNSDate1, someNSDate2, someNSDate3]
customerId = ["a1", "h5", "c2"]
age = [21, 8, 60]

How can I sort these arrays according to the elements in one of them?

For example: How can I make sure that customerId[0] = c2 and age[0] = 60 if I sort createdAt according to NSDate, and someNSDate3 is the most recent date?

KML
  • 2,302
  • 5
  • 26
  • 47
  • Parallel arrays are really error prone. Why not create a custom object with three properties and have one array of those? It makes keeping the proper relationship for the data elements a lot easier. – Phillip Mills Nov 03 '15 at 14:09
  • Yes, if you could show how that would be a great answer – KML Nov 03 '15 at 14:11
  • It's not clear to me what part of this you don't understand. How to create a custom class (or struct)? How to create an array? ...? – Phillip Mills Nov 03 '15 at 14:14
  • How to create a struct, and then add data to it, just like I could add elements to an array by looping over a dictionary and appending the values for example – KML Nov 03 '15 at 14:16
  • 3
    Your problem is that you have [given up on the classic solution](http://stackoverflow.com/questions/33481697/how-to-create-dictionary-from-json) of making arrays of dictionaries from JSON data and now you're stuck with subpar options: your objects have no way of relating to each other. // Take JSON, make dictionaries or structs/classes from JSON data. Then filter these objects, not lone arrays. – Eric Aya Nov 03 '15 at 14:17
  • That is exactly right...lol @eric-d ahhh, how I hate JSON/Dictionaries – KML Nov 03 '15 at 14:18
  • Or this one: http://stackoverflow.com/questions/29432656/how-can-i-sort-multiple-arrays-based-on-the-sorted-order-of-another-array. – Martin R Nov 03 '15 at 14:35
  • You are right @martin-r, I have been struggling with this for days. It was not clear to me before the answer by danielbross in this question that you could create arrays of Structs. *array.append(DataItem(, , ));* You can mark as duplicate – KML Nov 03 '15 at 14:41
  • [link](http://stackoverflow.com/questions/33485794/sorting-several-arrays-according-to-one-array/33486226#33486226) – user3441734 Nov 03 '15 at 14:52

2 Answers2

2

I would suggest making a struct containing all the data instead.

struct DataItem {
    let createdAt: NSDate
    let customerId: Int
    let age: Int
}

And then create an array of DataItems

EDIT: create an array with DataItems like this

var array : [DataItem]()

then you can add new DataItems like this

array.append(DataItem(<the date>, <the customerid>, <age>));
stonecompass
  • 547
  • 9
  • 27
1

Here you go, a complete example including sorting:

struct Item {
    let creationDate : NSDate
    let customerID : String
    let age : Int
}

let items = [
    Item(creationDate: NSDate(), customerID: "a1", age: 21),
    Item(creationDate: NSDate(), customerID: "h5", age: 8),
    Item(creationDate: NSDate(), customerID: "c2", age: 60)
]

extension NSDate : Comparable {}
public func <(lhs: NSDate, rhs: NSDate) -> Bool {
    return lhs.compare(rhs) == .OrderedAscending
}

let dateSorted = items.sort{ $0.creationDate < $1.creationDate }
let idSorted = items.sort{ $0.customerID < $1.customerID }
let ageSorted = items.sort{ $0.age < $1.age }
Kametrixom
  • 14,673
  • 7
  • 45
  • 62