Latest 0.2.0
Homepage https://github.com/naoto0n2/Sorter
License MIT
Platforms ios 8.0
Authors

Version
License
Platform

Sorter with multiple conditions in Swift, such as NSSortDescriptor.

let items: [Item] = ...

// Create Sorter for each attribute
let sorterForNum = Sorter<Item>(asc: false) { $0.num }
let sorterForText = Sorter<Item>(asc: true) { $0.text }

// Create Sorter with multiple conditions
let sorter = Sorter([sorterForNum, sorterForText])

// Get sorted result
let result = items.sorted(by: sorter)

/*
result: [
    Item(num: 2, text: "a"),
    Item(num: 2, text: "b"),
    Item(num: 2, text: "c"),
    Item(num: 1, text: "a"),
    Item(num: 1, text: "b"),
    Item(num: 1, text: "c")
]
*/

Initializer

1. Initialize Sorter by comparable attribute

Default order is ascending.

Sorter<Item>(asc: false) { $0.num } // Pattern 1
Sorter<Item>(asc: true) { $0.num } // Pattern 2
Sorter<Item> { $0.num } // Pattern 3

2. Initialize Sorter by descriptor

Sorter<Item> { $0.0.text < $0.1.text }

3. Initialize Sorter by multiple conditions

Sorter([sorter1, sorter2])
Sorter([sorter1, sorter2, sorter3])
...

Sort sequence

Sort items by sorter

let result = items.sort(by: sorter)

Requirements

  • Swift 3.0 or later
  • iOS 8.0 or later

Installation

CocoaPods

  • Insert pod 'Sorter' to your Podfile.
  • Run pod install.

Note: CocoaPods 1.1.0 is required to install Sorter.

Author

Naoto Onagi, [email protected]

License

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

Latest podspec

{
    "name": "Sorter",
    "version": "0.2.0",
    "summary": "Sorter with multiple conditions in Swift, such as NSSortDescriptor.",
    "homepage": "https://github.com/naoto0n2/Sorter",
    "authors": {
        "Naoto Onagi": "[email protected]"
    },
    "social_media_url": "https://twitter.com/naoto0n2",
    "platforms": {
        "ios": "8.0"
    },
    "source_files": "Sorter/Classes/**/*",
    "source": {
        "git": "https://github.com/naoto0n2/Sorter.git",
        "tag": "0.2.0"
    },
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "pushed_with_swift_version": "3.0"
}

Pin It on Pinterest

Share This