Git Product home page Git Product logo

dictionarysorting's Introduction

DictionarySorting

Xcode Swift GitHub tag (latest SemVer) GitHub

What

DictionarySorting is a Swift Package Manager package for iOS/tvOS (10.0 and above), watchOS (4.0 and above), and macOS (10.14 and above), under Swift 5.0 and above, implementing an extension to Dictionary that provides an easy-to-use and ergonomic "swifty" way to sort a dictionary, as easy as:

let data = /* ... */ // a dictionary of some kind
let sorted = data.sorted(by: .keys(.ascending)) // returns an array of the elements stored in the
                                                // dictionary, sorted in ascending order of their keys

The package provides several such sorted(by:) functions, depending on whether one or both the dictionary's Key and Value types conform to Comparable:

extension Dictionary where Key: Comparable {
    
    public typealias KeyOrder = SortOrder
    
    public enum KeySorter {
        case keys(KeyOrder)
    }
    
    public func sorted(by sorter: KeySorter) -> [Element]
    
}

extension Dictionary where Value: Comparable {
    
    public typealias ValueOrder = SortOrder
    
    public enum ValueSorter {
        case values(ValueOrder)
    }

    /// Keys will not be in a well-defined order.
    public func sorted(by sorter: ValueSorter) -> [Element]
    
}

extension Dictionary where Key: Comparable, Value: Comparable {
    
    public enum KeyValueSorter {
        case keysOnly(KeyOrder)
        case valuesOnly(ValueOrder) // Keys will not be in a well-defined order.
        case valuesThenKeys(values: ValueOrder, keys: KeyOrder)
    }

    public func sorted(by sorter: KeyValueSorter) -> [Element]
    
}

Note that sorting by values without also sorting by keys does not guarantee a stable order for the keys. For example,

let data = [5: 70, 6: 70, 7: 60, 8: 50]
let valueSortedData = data.sorted(by: .valuesOnly(.ascending))

may result in either of these arrays:

[
    (key: 8, value: 50),
    (key: 7, value: 60),
    (key: 5, value: 70),
    (key: 6, value: 70),
]

and

[
    (key: 8, value: 50),
    (key: 7, value: 60),
    (key: 6, value: 70),
    (key: 5, value: 70),
]

The same is true with

let data = [5: 70, 6: 70, 7: 60, 8: 50]
let valueSortedData = data.sorted(by: .values(.ascending))

Note also that there's no option

case keysThenValues(keys: KeyOrder, values: ValueOrder)

in the KeyValueSorter enumeration

public enum KeyValueSorter {
    case keysOnly(KeyOrder)
    case valuesOnly(ValueOrder) // Keys will not be in a well-defined order.
    case valuesThenKeys(values: ValueOrder, keys: KeyOrder)
}

because the keys in a dictionary are guaranteed to be unique and, therefore, once the dictionary is sorted by its keys, its values will be fixed and their order can no longer change.

The package also provides some useful global functions:

public func sort<K: Hashable & Comparable, V>(_ subseq: Dictionary<K, V>.SubSequence,
                                              _ sorter: Dictionary<K, V>.KeySorter)
    -> [Dictionary<K, V>.Element]

public func sort<K: Hashable & Comparable, V>(_ kvPairs: [Dictionary<K, V>.Element],
                                              _ sorter: Dictionary<K, V>.KeySorter)
    -> [Dictionary<K, V>.Element]

public func sort<K: Hashable, V: Comparable>(_ subseq: Dictionary<K, V>.SubSequence,
                                             _ sorter: Dictionary<K, V>.ValueSorter)
    -> [Dictionary<K, V>.Element]

public func sort<K: Hashable, V: Comparable>(_ kvPairs: [Dictionary<K, V>.Element],
                                             _ sorter: Dictionary<K, V>.ValueSorter)
    -> [Dictionary<K, V>.Element]

public func sort<K: Hashable & Comparable, V: Comparable>(_ subseq: Dictionary<K, V>.SubSequence,
                                                          _ sorter: Dictionary<K, V>.KeyValueSorter)
    -> [Dictionary<K, V>.Element]

public func sort<K: Hashable & Comparable, V: Comparable>(_ kvPairs: [Dictionary<K, V>.Element],
                                                          _ sorter: Dictionary<K, V>.KeyValueSorter)
    -> [Dictionary<K, V>.Element]

Dependencies

DictionarySorting depends on two other libraries of mine, SortOrder and DictionarySlicing.

Installation

DictionarySorting is provided only as a Swift Package Manager package, because I'm moving away from CocoaPods and Carthage, and can be easily installed directly from Xcode.

License

DictionarySorting is available under the MIT license. See the LICENSE file for more info.

dictionarysorting's People

Contributors

wltrup avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.