NEW: My new book Pro SwiftUI is out now – level up your SwiftUI skills today! >>

SOLVED: Unable to sort array in asc order - Hacking With Swift Project 1 challenge UIKit

Forums > Books

Hey all,

Regarding the challenge at the end of Project 1 of UIKit,

Attempting to sort the array my files in ascending order but Swift fails to build, can anyone see where I'm going wrong?


//
//  ViewController.swift
//  hws_project1
//
//  Created by mojo on 15/04/2020.
//  Copyright © 2020 m. All rights reserved.
//

import UIKit

class ViewController: UITableViewController {

    var pictures = [String]()

    override func viewDidLoad() {
        super.viewDidLoad()

        title = "Storm Viewer"
        navigationController?.navigationBar.prefersLargeTitles = true

        let fm = FileManager.default
        let path = Bundle.main.resourcePath!

        let items = try! fm.contentsOfDirectory(atPath: path)

        for item in items {
            if item.hasPrefix("nssl"){
                // this is a pic to load
                pictures.append(item)
            }
        }

        //this should order the pics in ascending order but doesnt work
        picturesArray.sort(by: > )

    }
    // How many rows should appear in the table
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return pictures.count
    }
    // What each row should look like
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Picture", for: indexPath)
        cell.textLabel?.text = pictures[indexPath.row]
        return cell
    }

    // 1. try loading the vc
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if let vc = storyboard?.instantiateViewController(withIdentifier: "Detail") as? DetailViewController {
            // 2. set its selectedImage property
            vc.selectedImage = pictures[indexPath.row]

            // 3. push it on the navigation controller
            navigationController?.pushViewController(vc, animated: true)

        }
    }

    // Array of pics listed in the Content folder
    var picturesArray = ["nssl0049.jpg", "nssl0046.jpg", "nssl0091.jpg", "nssl0045.jpg", "nssl0051.jpg", "nssl0041.jpg", "nssl0042.jpg", "nssl0043.jpg", "nssl0033.jpg", "nssl0034.jpg"]

}

The code in comments where it's not working is for my own benefit so I don't lose track. it builds successfully but when you run the app in the sim, it's still displaying the files in the order I've said!

1      

So I tried:

        picturesArray = pictures.sorted(by: { (pic1: "nssl0049.jpg" , pic2: "nssl0046.jpg" ) -> Bool in
            return pic1 > pic2
        })

But this fails to build, I'm trying to run a few sims in Playground but no success

1      

With infered type, this works when I print it out:

picturesArray.sort()

But that's it so far, unsure how to alter the contents of FileManager so it's displayed how I wish to

1      

hi,

i'm not sure what your picturesArray is doing, because you define it; you attempt to sort it (the closure you attempted give for the sort is suspicious); but you never use it. the UITableView is driven by the pictures array and does not depend on picturesArray.

just add a .sorted() function call on the items in the for loop that appends the filenames to the pictures array (then, deep-six what you call picturesArray):

        let fm = FileManager.default
        let path = Bundle.main.resourcePath!
        let items = try! fm.contentsOfDirectory(atPath: path)
        for item in items.sorted() {
            if item.hasPrefix("nssl") {
                pictures.append(item)
            }
        }

alternatively, just sort the pictures array after it is flled with filenames:

        let fm = FileManager.default
        let path = Bundle.main.resourcePath!
        let items = try! fm.contentsOfDirectory(atPath: path)
        for item in items {
            if item.hasPrefix("nssl") {
                pictures.append(item)
            }
        }
        pictures.sort()

that should do it.

hope that helps, DMG

3      

Hey @delawaremathguy I've just added that line into the pictures load IF statement before I saw this and bloody hell it works, can't believe it's that simple! I feel like a fool!

1      

hi,

please do not worry about feeling foolish -- not only do i do something pretty silly every day, but i fully expect that i'll do something silly every day. the trick is to think it out a little and learn from it. that's what makes each of us better at this stuff.

good luck going forward, DMG

--

4      

Hacking with Swift is sponsored by Play

SPONSORED Play is the first native iOS design tool created for designers and engineers. You can install Play for iOS and iPad today and sign up to check out the Beta of our macOS app with SwiftUI code export. We're also hiring engineers!

Click to learn more about Play!

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.