-3

I'm getting data from a website in the form of NSString. The data may be just integers, sometimes integers with additional "a"/"b" representing additional information, or "#" meaning no data available. I've got the data into an array:

var labelMin = "-"
var labelMax = "-"

var arr = ["55a", "95a", "66", "25", "88b", "#"]

My question is:

How do I get the minimum & maximum value considering all objects in this array (e.g. also considering "95"in "95a", but no need to consider "#"), and print out subsequently on labelMin.text & labelMax.text?

[EDIT: The actual code will have 24 objects in the array, corresponding to data for every hour, which is continuously changing]

Allister Bah
  • 1,134
  • 3
  • 13
  • 19
  • @sasquatch I've deleted a similar question and reposted because there was a few edits on my question from others and someone mentioned my question wasn't clear enough. – Allister Bah May 19 '15 at 11:41

3 Answers3

1

This is CS 101 stuff.

Create int variables for max and min. Set min to Int.max and max to Int.min.

Write a for loop that loops through your array, taking the integer value of each item, and if it's less than min, save it to min, and if it's greater than max, save it to max.

Duncan C
  • 128,072
  • 22
  • 173
  • 272
1

@user3815344's answer works, however you can simplify it by using minElement and maxElement to retrieve the minimum and maximum values. For example:

let arr = ["55a", "95a", "66", "25", "88b", "#"]
let numbers: [Int] = arr.reduce([]) {
    if let num = "".join($1.componentsSeparatedByCharactersInSet(NSCharacterSet.decimalDigitCharacterSet().invertedSet)).toInt() {
        return $0 + [num]
    }

    return $0
}

minElement(numbers) // 25
maxElement(numbers) // 95

Updated for Swift 2: toInt has been replaced by a failable initialiser on Int that takes a String and minElement and maxElement have been replaced by protocol extensions:

let arr = ["55a", "95a", "66", "25", "88b", "#"]
let numbers: [Int] = arr.reduce([]) {
    if let num = Int("".join($1.componentsSeparatedByCharactersInSet(NSCharacterSet.decimalDigitCharacterSet().invertedSet))) {
        return $0 + [num]
    }

    return $0
}

let min: Int? = numbers.minElement() // 25
let max: Int? = numbers.maxElement() // 95
ABakerSmith
  • 22,759
  • 9
  • 68
  • 78
  • Thanks so much. This works! But could you explain briefly what ".join(item.componentsSeparatedByCharactersInSet(NSCharacterSet.decimalDigitCharacterSet( ).invertedSet)).toInt( )" means? – Allister Bah May 26 '15 at 13:14
  • 1
    [Here's](http://stackoverflow.com/questions/21653085/remove-all-non-numeric-characters-nsstring-keep-spaces) a question where the accepted answer has some detail about what's going on :) – ABakerSmith May 26 '15 at 13:43
  • Thanks. Regarding the above method again, what if I want to still keep 6 spaces in the numbers array? (Maybe let the 6th item be nil?) – Allister Bah May 27 '15 at 04:38
0
    var arr = ["55a", "95a", "66", "25", "88b", "#"]
    var minval = 0
    var maxval = 0
    for val in arr{
        var Number = "".join(val.componentsSeparatedByCharactersInSet(NSCharacterSet.decimalDigitCharacterSet().invertedSet))
        if Number.toInt() >= 0{
            if val == arr[0]{
                minval = Number.toInt()!
                maxval = minval
            } else{
                if Number.toInt() < minval{
                    minval = Number.toInt()!
                }
                if Number.toInt() > maxval{
                    maxval = Number.toInt()!
                }
            }
        }
    }
    println(maxval)
    println(minval)

The above code will work for your case

user3815344
  • 243
  • 1
  • 21