GithubHelp home page GithubHelp logo

takat6 / how-to-sort-dictionary-array Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 16 KB

Swiftで辞書型の配列をソートしたいとき(最近よく使う)

Swift 100.00%
swift5 array closure dictionary swift

how-to-sort-dictionary-array's Introduction

Swift5で辞書の配列をソートしたいときの対処法

下記のようなDictionary型を配列にした変数があるとする.....

// 環境
// swift 5.3

// var data: [[String: String]] = [ (型のシンタックスシュガー宣言)
var data: Array<Dictionary<String, String>> = [
    [ "num": "1", "val": "B"],
    [ "num": "0", "val": "A"],
    [ "num": "2", "val": "C"],
    [ "num": "5", "val": "F"],
    [ "num": "4", "val": "E"],
    [ "num": "3", "val": "D"]
]

上記の配列をnumをキーとしてソートしたいという地獄的な場面に遭遇した場合は下記の一行で昇順、降順にソートできる。

/ 昇順
data = data.sorted(by: {Int($0["num"]!)! < Int($1["num"]!)!})

// 降順
data = data.sorted(by: {Int($0["num"]!)! > Int($1["num"]!)!})

how-to-sort-dictionary-array's People

Contributors

takat6 avatar

Watchers

 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.