0

In my tableview I get my data shown from these arrays:

var offersProduct = [String]()
var offersPrice = [String]()
var savings = [String]()
var shops = [String]()
var imageNames = [String]()
var exDates = [Int]()
var pickedIDs = [String]()
var setDates = [Int]()

Right now the data isn't sorted in any way. So I have 2 questions.

  1. How do I sort arrays after date? Nearest to NSDate first.

I have the array of exDates, witch is an array of Int of timeIntervalSince1970. So i want the closest expiry date to get index 0 of the array.

  1. How do I sort the other arrays like the first one?

So, lets say I have sorted exDates array, that gives me another problem, cause now the expiry date shown, doesn't match with the data from the other arrays. So I need to somehow sort the other arrays, by moving the data in from the same index as in the exDates array. How can that be done?

Hope someone can help.

1
  • 1
    You would really benefit from watching talk. youtube.com/watch?v=c3Kg3c8vqsc @RobNapier Talks about exactly the problem you're running into, and many others. Commented Nov 23, 2017 at 8:29

2 Answers 2

4

Your problem is that you use separate entities where single/uniform entity should be used. To achieve what you are trying, you should switch to using class or struct which would encompass all the associated data. If you proceed with the approach you currently adopt, you are guaranteed to have arbitrary bugs and performing dull operations to synchronize data across all your arrays. So, in essence, having multiple arrays instead of one array of specialized objects is a bad practice.

Consider refactoring your arrays:

struct Entity {
    var offerProduct : String
    var offerPrice : String
    var savings : String
    var shop : String
    var imageName : String
    var exDate : Int
    var pickedID : String
    var setDate : Int
}

var entities = [Entity]()

Then to sort single array of entities (once filled), you merely set necessary criteria, such as:

entities = entities.sorted(by: {$0.exDate > $1.exDate })
Sign up to request clarification or add additional context in comments.

1 Comment

Almost synchronously :-)
4

Make a structure:

struct Entry {
  let product: String
  let price: String
  let saving: String 
  let shop: String 
  let imageName: String 
  let exDate: Int 
  let pickedID: String 
  let setDate: Int
}

Then use one array of these structures instead of several:

var entries = [Entry]()

You can sort this array easily by one of the structure's fields:

let expiryTimePoint = ...
entries = entries.sorted({ 
    // compare absolute differences of intervals for sorting
    return abs($0.exDate - expiryTimeInterval) > abs($1.exDate - expiryTimeInterval) 
})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.