Sort a string array in Swift using a function

1

I need help to solve the following programming logic problem in Swift.

The task is to organize the array below in descending order using a function.

Description: You must implement an algorithm that receives a list of strings with specific format and returns it sorted in descending order. Each string will be a month in which the TAG delivered its boxes to club members (eg "January 2017", "March 2018", "April 2016"). The algorithm should be a function, written in Swift 4.x, whose call should be done in the following way:

let mesesTag = [ "janeiro/2018", "novembro/2017", "fevereiro/2018", "março/2017", "julho/2016", "março/2018" ]

let mesesTagOrdenados = ordenar(mesesTag)

print(mesesTagOrdenados)

// resultado para este exemplo deve ser: // [ "março/2018", "fevereiro/2018", "janeiro/2018", "novembro/2017", "março/2017", "julho/2016" ]
    
asked by anonymous 04.05.2018 / 15:02

3 answers

5

Dear Rodrigo,

The activity developed by your instructor aims to improve your knowledge and will face me, this is very important in our area. Do not feel satisfied to receive the solution to a problem, so you learn nothing and become a developer ctr + c, ctr + v, dude, try to do, put the code here that we will be happy to help you, but try to do .. ..

    
09.05.2018 / 20:41
3

Try this:

import UIKit

// MARK: - Funções

func ordenar(_ meses: [String]) -> [String] {

    let df = DateFormatter(dateFormat: "MMMM/yyyy")
    let mesesEmData = meses.map { (mes) -> Date? in df.date(from: mes) }
    let mesesEmDataSemNulos = mesesEmData.flatMap { $0 }

    let mesesOrdenados = mesesEmDataSemNulos.sorted(by: { (mes1, mes2) -> Bool in
        mes1.compare(mes2) == .orderedAscending
    })

    return mesesOrdenados.map { df.string(from: $0) }
}

extension DateFormatter {
    convenience init(dateFormat: String) {
        self.init()
        self.dateFormat = dateFormat
        self.locale = Locale(identifier: "pt_BR")
    }
}

// MARK: - Questão

let mesesTag = [ "janeiro/2018", "novembro/2017", "fevereiro/2018", "março/2017", "julho/2016", "março/2018" ]

let mesesTagOrdenados = ordenar(mesesTag)

print(mesesTagOrdenados)

Log:

["julho/2016", "março/2017", "novembro/2017", "janeiro/2018", "fevereiro/2018", "março/2018"]
    
05.05.2018 / 15:49
0

I agree with Rafael Leão that you should put your code to help you solve your problem, but since you had free time, here it goes:

let mesesTag = [ "janeiro/2018", "novembro/2017", "fevereiro/2018", "março/2017", "julho/2016", "março/2018" ]       
let mesesTagOrdenados = ordenar(mesesTag)
print(mesesTagOrdenados)

enum months: String {
    case janeiro, fevereiro, marco, abril, maio, junho, julho, agosto, setembro, outubro, novembro, dezembro
}

func ordenar(_ meses: [String]) -> [String] {
    var splittedArray = [[String]]()
    for mes in meses {
        let splitted = mes.split(separator: "/")
        let first = String(splitted[0])
        let second = String(splitted[1])
        splittedArray.append([first, second])
    }

    let yearOrdered = splittedArray.sorted(by: sortByYear)
    let monthOrdered = yearOrdered.sorted(by: sortByMonth)

    let result = monthOrdered.map { (item) -> String in
        item.joined(separator: "/")
    }

    return result
}

func sortByYear(_ s1: [String], _ s2: [String]) -> Bool {
    let anoS1 = s1.last!
    let anoS2 = s2.last!

    if anoS1 > anoS2 {
        return true
    } else {
        return false
    }
}

func sortByMonth(_ s1: [String], _ s2: [String]) -> Bool {
    let mesS1 = months(rawValue: s1.first!.lowercased().replacingOccurrences(of: "ç", with: "c"))!.hashValue
    let mesS2 = months(rawValue: s2.first!.lowercased().replacingOccurrences(of: "ç", with: "c"))!.hashValue
    let anoS1 = s1.last!
    let anoS2 = s2.last!

    if mesS1 > mesS2 && anoS1 >= anoS2 {
        return true
    } else {
        return false
    }
}
    
04.05.2018 / 21:50