swift2 - Sorting a struct array or dictionary in Swift -
i have struct dictionary (taken swift: how declare 2d array (grid or matrix) in swift allow random insert , @rintaro ):
struct matrix2d<keyelem:hashable, value> { var _storage:[keyelem:[keyelem:value]] = [:] subscript(x:keyelem, y:keyelem) -> value? { { return _storage[x]?[y] } set(val) { if _storage[x] == nil { _storage[x] = [:] } _storage[x]![y] = val } } }
now sort dictionary x, can't find way achieve this. event possible sort dictionary? or should maybe use solution array instead of dictionary?
struct matrix2d<t> { var _storage:[[t?]] = [] subscript(x:int, y:int) -> t? { { if _storage.count <= x { return nil } if _storage[x].count <= y { return nil } return _storage[x][y] } set(val) { if _storage.count <= x { let cols = [[t?]](count: x - _storage.count + 1, repeatedvalue: []) _storage.extend(cols) } if _storage[x].count <= y { let rows = [t?](count: y - _storage[x].count + 1, repeatedvalue: nil) _storage[x].extend(rows) } _storage[x][y] = val } } }
thanks help!
a dictionary per definition collection type containing unordered key - value pairs.
there solutions ordered dictionaries using backing array, see example earning-swift-ordered-dictionaries
Comments
Post a Comment