按日期对数组中的对象进行排序

时间:2021-05-21 16:01:43

I have an array containing an object called HistoryObject and it has properties such as "date", "name", etc.

我有一个包含名为HistoryObject的对象的数组,它具有“日期”、“名称”等属性。

I am sorting the array like so:

我对数组进行排序:

 let sortedArray = HistoryArray.sort({ $0.date.compare($1.date) == NSComparisonResult.OrderedDescending})

which is supposed to sort the date from newer to oldest. For example:

从更新到最老的日期。例如:

  • Jun 30, 2016
  • 2016年6月30日
  • Jun 29, 2016
  • 2016年6月29日,

etc..

等。

But when my array contains "Jul 2, 2016" the sorted array becomes:

但当我的数组包含“2016年7月2日”时,排序后的数组变为:

  • Jun 30, 2016
  • 2016年6月30日
  • Jun 29, 2016
  • 2016年6月29日,
  • Jul 2, 2016
  • 2016年7月2日

Where "Jul 2, 2016" should be on top after sorting, now it's on the bottom? How can I fix this problem?

“2016年7月2日”在排序后应该在上面,现在它在底部?我如何解决这个问题?

3 个解决方案

#1


34  

Using Swift 4 & Swift 3

使用Swift 4和Swift 3

let testArray = ["25 Jun, 2016", "30 Jun, 2016", "28 Jun, 2016", "2 Jul, 2016"]
var convertedArray: [Date] = []

var dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd MM, yyyy"// yyyy-MM-dd"

for dat in testArray {
    let date = dateFormatter.date(from: dat)
    if let date = date {
        convertedArray.append(date)
    }
}

var ready = convertedArray.sorted(by: { $0.compare($1) == .orderedDescending })

print(ready)

Using Swift 2

使用快速2

For example you have the array with dates and another 1 array, where you will save the converted dates:

例如,您有日期和另一个数组的数组,您将保存转换后的日期:

var testArray = ["25 Jun, 2016", "30 Jun, 2016", "28 Jun, 2016", "2 Jul, 2016"]
var convertedArray: [NSDate] = []

After that we convert the dates:

之后我们将日期转换为:

var dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "dd MM, yyyy"// yyyy-MM-dd"

for dat in testArray {
    var date = dateFormatter.dateFromString(dat)
    convertedArray.append(date!)
}

And the result:

结果:

var ready = convertedArray.sort({ $0.compare($1) == .OrderedDescending })

print(ready)

#2


12  

For Swift 3

斯威夫特3

var testArray = ["25 Jun, 2016", "30 Jun, 2016", "28 Jun, 2016", "2 Jul, 2016"]
var convertedArray: [Date] = []

var dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd/MM/yyyy"

for dat in testArray {
    var date = dateFormatter.date(from: dat)
    convertedArray.append(date!)
}

//Approach : 1
convertedArray.sort(){$0 < $1}    

//Approach : 2
convertedArray.sorted(by: {$0.timeIntervalSince1970 < $1.timeIntervalSince1970})

print(convertedArray)

#3


2  

Avoiding extra variable of convertedArray

避免额外变量的收敛

Using Swift 4 & Swift 3

使用Swift 4和Swift 3

let testArray = ["25 Jun, 2016", "30 Jun, 2016", "28 Jun, 2016", "2 Jul, 2016"]

var dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd MM, yyyy"// yyyy-MM-dd"

var ready = convertedArray.sorted(by: { dateFormatter.date(from:$0).compare(dateFormatter.date(from:$1)) == .orderedDescending })

print(ready)

#1


34  

Using Swift 4 & Swift 3

使用Swift 4和Swift 3

let testArray = ["25 Jun, 2016", "30 Jun, 2016", "28 Jun, 2016", "2 Jul, 2016"]
var convertedArray: [Date] = []

var dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd MM, yyyy"// yyyy-MM-dd"

for dat in testArray {
    let date = dateFormatter.date(from: dat)
    if let date = date {
        convertedArray.append(date)
    }
}

var ready = convertedArray.sorted(by: { $0.compare($1) == .orderedDescending })

print(ready)

Using Swift 2

使用快速2

For example you have the array with dates and another 1 array, where you will save the converted dates:

例如,您有日期和另一个数组的数组,您将保存转换后的日期:

var testArray = ["25 Jun, 2016", "30 Jun, 2016", "28 Jun, 2016", "2 Jul, 2016"]
var convertedArray: [NSDate] = []

After that we convert the dates:

之后我们将日期转换为:

var dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "dd MM, yyyy"// yyyy-MM-dd"

for dat in testArray {
    var date = dateFormatter.dateFromString(dat)
    convertedArray.append(date!)
}

And the result:

结果:

var ready = convertedArray.sort({ $0.compare($1) == .OrderedDescending })

print(ready)

#2


12  

For Swift 3

斯威夫特3

var testArray = ["25 Jun, 2016", "30 Jun, 2016", "28 Jun, 2016", "2 Jul, 2016"]
var convertedArray: [Date] = []

var dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd/MM/yyyy"

for dat in testArray {
    var date = dateFormatter.date(from: dat)
    convertedArray.append(date!)
}

//Approach : 1
convertedArray.sort(){$0 < $1}    

//Approach : 2
convertedArray.sorted(by: {$0.timeIntervalSince1970 < $1.timeIntervalSince1970})

print(convertedArray)

#3


2  

Avoiding extra variable of convertedArray

避免额外变量的收敛

Using Swift 4 & Swift 3

使用Swift 4和Swift 3

let testArray = ["25 Jun, 2016", "30 Jun, 2016", "28 Jun, 2016", "2 Jul, 2016"]

var dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd MM, yyyy"// yyyy-MM-dd"

var ready = convertedArray.sorted(by: { dateFormatter.date(from:$0).compare(dateFormatter.date(from:$1)) == .orderedDescending })

print(ready)