質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

Q&A

解決済

1回答

2238閲覧

swift3 配列をソートするときのエラー

kifu

総合スコア26

Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

0グッド

0クリップ

投稿2016/12/25 03:32

次の__配列を降順にソートしたい__ので、下記の3通りやってみましたが、エラーが出てしまいます。どのようにすれば降順にソートできるのでしょうか。御回答に感謝します。

1 [配列]
var sectionTitle = ["H28,01,23","H27,12,31","H28,01,12","H28,02,21","H28,11,10"]

2 試したこと3つ、とエラーの内容
① let sortedST = sectionTitle.sorted{ $0 > $1 }
エラー1 Cannot use instance member 'sectionTitle' within property initializer; property initializers run before 'self' is available

② func sortedsectionTitle2(sectionTitle : String) -> String
{let sortedST2 = sectionTitle.sorted{ $0 > $1 }
return sortedST2}

エラー2 Value of type 'String' has no member 'sorted'

③ func sortedsectionTitle(sectionTitle : Array<Any>) -> Array<Any>
{let sortedST1 = sectionTitle.sorted{ $0 > $1 }
return sortedST1 }
エラー3 Binary operator '>' cannot be applied to two 'Any' operands

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

①のパターンで普通にできていると思いますが、、、こちらではエラーはありませんでした。

以下のコードをPlaygroundで試しました。

swift

1import UIKit 2 3var sectionTitle = ["H28,01,23", "H27,12,31", "H28,01,12", "H28,02,21", "H28,11,10"] 4 5let sortedST = sectionTitle.sorted{ $0 > $1 } 6sortedST 7 8func sortedsectionTitle2(sectionTitle : [String]) -> [String] { 9 let sortedST2 = sectionTitle.sorted{ $0 > $1 } 10 return sortedST2 11} 12 13let sortedST1 = sortedsectionTitle2(sectionTitle: sectionTitle)

☆ 結果スクショ
d

①のパターンのエラーはよく見ると変数の初期化時に他の変数を参照している時に出るエラーでした、
なので以下の様に記述してみてください。

swift

1import UIKit 2 3class ViewController: UIViewController { 4 5 var sectionTitle = ["H28,01,23","H27,12,31","H28,01,12","H28,02,21","H28,11,10"] 6 7 override func viewDidLoad() { 8 super.viewDidLoad() 9 10 let sortedST = sectionTitle.sorted{ $0 > $1 } 11 print(sortedST) 12 } 13}

投稿2016/12/25 04:20

編集2016/12/25 04:26
_Kentarou

総合スコア8490

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

kifu

2016/12/25 10:16 編集

メソッドの中では、エラーになりませんが結局ソートされません。下記コードでは、どこに問題があるのでしょうか。     import UIKit class ViewController : UIViewController, UITableViewDelegate, UITableViewDataSource { //// func numberOfSections(in tableView: UITableView) -> Int // Default is 1 if not implemented { return sectionTitle.count } //// func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? // fixed font style. use custom view (UILabel) if you want something different { return sectionTitle[section] } // ステータスバーの高さ、上にカレンダーかデイトピッカーを置く。 let statusBarHeight = UIApplication.shared.statusBarFrame.height + 200 //セクションの項目 // let sortedTitle = sectionTitle.sort {$0 < $1 } var sectionTitle = ["H28,01,23","H27,12,31","H28,01,12","H28,02,21","H28,11,10"] // チェックリストの項目とチェック状態 var checkListItem1: [String : Bool] = [ "アイテム1" : true, "アイテム2" : false, "アイテム3" : true, "アイテム4" : true, "アイテム5" : false ] var checkListItem2: [String : Bool] = [ "アイテム2-1" : false, "アイテム2-2" : true, "アイテム2-3" : true, "アイテム2-4" : true, "アイテム2-5" : false ] var checkListItem3: [String : Bool] = [ "アイテム3-1" : true, "アイテム3-2" : true, "アイテム3-3" : true, "アイテム3-4" : true, "アイテム3-5" : false ] var checkListItem4: [String : Bool] = [ "アイテム4-1" : true, "アイテム4-2" : false, "アイテム4-3" : true, "アイテム4-4" : false, "アイテム4-5" : false ] var checkListItem5: [String : Bool] = [ "アイテム5-1" : true, "アイテム5-2" : false, "アイテム5-3" : true, "アイテム5-4" : true, "アイテム5-5" : true ] // var tableData = [checkListItem1, checkListItem2, checkListItem3, checkListItem4, checkListItem5] var tableData: [[String: Bool]] = [] let tableView = UITableView() // override func viewDidLoad() { super.viewDidLoad() tableData = [checkListItem1, checkListItem2, checkListItem3, checkListItem4, checkListItem5] // UITableView の作成 tableView.frame = CGRect( x: 0, y: statusBarHeight, width: self.view.frame.width, height: self.view.frame.height - statusBarHeight ) tableView.delegate = self tableView.dataSource = self self.view.addSubview(tableView) } // セルの作成 // func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { // tableDataの中から抽出 let sectionData = tableData[indexPath.section] // キーで並び替え let keys = sectionData.keys.sorted() // キーの文字列を取得 let cellText = keys[indexPath.row] // セルの作成とテキストの設定 let cell = UITableViewCell(style: .default, reuseIdentifier: "cell") cell.textLabel?.text = cellText /// let cellIscheckd = sectionData[cellText] // チェック状態が true なら、初めからチェック状態にする if cellIscheckd == true { cell.imageView?.image = UIImage(named: "checked") } else { cell.imageView?.image = UIImage(named: "unchecked") } return cell } // セルがタップされた時の処理 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { if let cell = tableView.cellForRow(at: indexPath) { // タップしたセルのテキストを取得 let cellText = cell.textLabel?.text ?? "" // 画像を切り替えと Dictonary の値を変更 if cell.imageView?.image == UIImage(named: "checked") { self.tableData[indexPath.section][cellText] = false cell.imageView?.image = UIImage(named: "unchecked") } else { self.tableData[indexPath.section][cellText] = true cell.imageView?.image = UIImage(named: "checked") } // 選択状態を解除 cell.isSelected = false } } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { let sectionData = tableData[section] return sectionData.count } }
_Kentarou

2016/12/25 10:27

viewDidLoad()の最初に以下のコードを追加してみてください。 sectionTitle = sectionTitle.sorted { $0 > $1 }
kifu

2016/12/25 23:36

ありがとうございました。うまくいきました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問