Sorting an array of Dictionaries in swift and getting operator can't be applied to Any

Roggie

I am trying to sort an array of Dictionaries by their timeStamp value from newest to oldest

error: Binary operator '<' cannot be applied to two 'Any?' operands

timeStamp value is saved as an Int into the Dictionary:

timeStamp = inviteDict["timeStamp"] as? Int

Dictionary:

[
"type": "invite",
"complimentId": key,
"status": status,
"timeStamp": timeStamp!,
 "fromUser": user
]

Function - combines two arrays of tuples, I need to sort the array by timeStamp before passing via handler:

    func getInvitesAndCompliments(forUserId forId: String, handler: @escaping ([[String : Any]], Bool) -> ()){

            var invitesAndCompliments = [[String : Any]]()

            var invites = [[String : Any]]()

            var compliments = [[String : Any]]()

            getComplimentsReceived(forUserId: forId) { (complimentsDict, success) in
                invitesAndCompliments.removeAll()
                compliments = complimentsDict
                invitesAndCompliments = invites + compliments
                handler(invitesAndCompliments, true)
            }

            getInvitesReceived(forUserId: forId) { (invitesDict, success) in
                invitesAndCompliments.removeAll()
                invites = invitesDict
                invitesAndCompliments = compliments + invites

                //sort array by timeStamp value
                let sorted_invitesAndCompliments = invitesAndCompliments.sorted(by: { $0["timeStamp"] < $1["timeStamp"] })

                handler(sorted_invitesAndCompliments, true)
            }


        }//end func
vadian

The result of getting a value from a dictionary is always Any?, you have to downcast the types

let sorted_invitesAndCompliments = invitesAndCompliments.sorted(by: { ($0["timeStamp"] as! Int) < $1["timeStamp"] as! Int })

To avoid that use a custom struct.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Sorting array of dictionaries with Any value

Sorting an array of dictionaries in Swift 2

Can't add dictionaries with nil values to array in swift

Binary operator '==' cannot be applied to operands of type 'Any?' and 'String' Swift iOS

Can't operator == be applied to generic types in C#?

Operator '&&' can't be applied to operands of type 'int' and 'bool'

Operator '+' can't be applied to operands of types 'decimal' and 'double' - NCalc

binary operator "<<" can't be applied to the expressions of type "wostream" and "string"

Expression.Error: Can´t be applied the * operator to List and number Types

Why can't the ??-operator be applied to generic types, but a null-check can be applied?

Object is Equatable, but still getting Binary operator '==' cannot be applied to two 'any Hashable & Identifiable' operands

Java : To write a method that can be applied on any kind of array

Getting "Binary operator ~= cannot be applied to two (Int, Int) operands" in Swift switch statement with (Int, Int) tuple cases

Can't getItem - not getting back any results

Sort an array of dictionaries in Swift

Swift Codable: Array of Dictionaries

Searchbar an array of Dictionaries Swift

Is there any algorithm that can be applied to this program?

Array Sorting getting error

Sorting an array of dictionaries based on dictionary values which are of Date type, without knowing the keys, in Swift 5.1?

Can't access nested dictionaries/arrays from json with swift

Getting a json array from dictionaries

swift binary operator cannot be applied to operands

Swift - Binary operator cannot be applied to operands of type

Swift 3.0 binary operator '==' cannot be applied

Binary operator '!=' cannot be applied to two ListNode swift

Why I can't create Array<[String : Any]>? Work with Alamofire (Swift)

Operator '==' cannot be applied to operands of type 'T' and 'T'

Sorting Struct -> Binary operator '<' cannot be applied to two 'String?' operands