2

I currently have a tuple array whose's first value is of type String and second value of type Double. The first value contains a date, and is in the format "MM/dd/yy".

I'm not using Date objects due to confliction in my code.

I'm having trouble sorting this array by String in descending order (recent to oldest) because of the MM.

For example, my array contains the following:

[("07/30/17", 5.0), ("08/30/17", 9.0), ("05/30/17", 5.0), ("05/28/17", 6.0)]

Calling tupleArray = tupleArray.sort( {$0.0 > $1.0} ) would produce the following:

[("08/30/17", 9.0), ("07/30/17", 5.0), ("05/30/17", 5.0), ("05/28/17", 6.0)]

That works as expected.

However, if I add a String entry "08/30/16", calling the same sort function produces the following:

[("08/30/17", 9.0), ("08/30/16", 30.0), ("07/30/17", 5.0), ("05/30/17", 5.0), ("05/28/17", 6.0)]

"08/30/16" is in the 2nd index when it should be the last index, because the year 16 is older then 17.

I've looked at:

  1. (Swift 3) Trying to sort an array of class objects by Date in swift 3?

  2. Sort Objects in Array by date

What am I doing wrong?

Thanks!

4
  • Sorting date strings is ugly and cumbersome. Think object-oriented, instead of the tuple use a custom struct or class with a stored Date (yes!) property and a computed property for the string representation. It makes life a lot easier. Commented Jul 30, 2017 at 19:16
  • @vadian I understand, but using the alternative Date does not work well with my current implementation...is there a better solution to sort the date Strings then just switching to Date objects? Commented Jul 30, 2017 at 19:18
  • 2
    Then consider to change your implementation. Don't fight the framework. Commented Jul 30, 2017 at 19:20
  • You could write your own custom sorting logic for your specific string representation of dates, but as @vadian already said before, changing your implementation to work with Date objects is the easier and better solution. Commented Jul 30, 2017 at 19:37

2 Answers 2

4
import Foundation

var tupleArray = [("08/30/17", 9.0), ("08/30/16", 30.0), ("07/30/17", 5.0), ("05/30/17", 5.0), ("05/28/17", 6.0)]

let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MM/dd/yy"

tupleArray = tupleArray.sorted { 
    guard let date1 = dateFormatter.date(from:$0.0), let date2 = dateFormatter.date(from:$1.0) else { return false}
    return date1 > date2
}

print(tupleArray)
Sign up to request clarification or add additional context in comments.

Comments

0

I would suggest your creating an Object that would have your String and Double as properties. And you can make a Data as a computed property which will be computed on String value. And do sorting using the Date computed property.

I'm sure this will make your life much easier and re-rewriting your code from tuples to objects. Later on it will be really hard to find the bug while sorting Strings.. I'm sure that your first case when there were 3 tuples in an array was just a coincidence that it sorted it in a right way (this is a guess)..

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.