Nick89
Nick89

Reputation: 2998

How to put an array in alphabetical order for UIPickerView in Swift

So I have an array made of strings, and when the user presses on my textfield, it shows a PickerView using the array.

I would like the PickerView to show the contents of the array in alphabetical order. But I have no idea how to do this, and cannot find anywhere online how to do it in Swift. Can someone please help?

Thanks in advance!

MY CODE:

import UIKit

class ViewController: UIViewController, UITextFieldDelegate, UIPickerViewDataSource, UIPickerViewDelegate {

@IBOutlet var questionLabel: UILabel!

@IBOutlet var buttonLabel: UIButton!

@IBOutlet var myBackgroundView: UIImageView!

@IBOutlet var questionTextField: UITextField!

let questions = ["Where are you going?", "Which city?", "When do you go?"]

var currentQuestionIndex = 0

let placeholder = ["Country", "City", "Date"]

var currentPlaceholderIndex = 0

@IBAction func nextButton(sender: AnyObject) {

// Initial setup on button press
questionTextField.hidden = false

barImage.hidden = false

// Reset text field to have no text
questionTextField.text = ""

// Displays the questions in array and displays the placeholder text in the textfield
if currentQuestionIndex <= questions.count && currentPlaceholderIndex <= placeholder.count {

    questionLabel.text = questions[currentQuestionIndex]
    questionTextField.placeholder = placeholder[currentPlaceholderIndex]

    currentQuestionIndex++

    currentPlaceholderIndex++

    buttonLabel.setTitle("Next", forState: UIControlState.Normal)


    // Animate text for questionLabel
    UIView.animateWithDuration(1.0, delay: 0.0, usingSpringWithDamping: 0.9, initialSpringVelocity: 0.5, options: nil, animations: {

        self.questionLabel.center = CGPoint(x: -110 , y: 305 + 20)

        }, completion: nil)

} else {

    performSegueWithIdentifier("countdownSegue", sender: self)

    //Add some logic here to run whenever the user has answered all the questions.

}

}

var countryPicker = ["France", "Germany", "Spain", "Northern Ireland", "Austria"]

var sortedArray = sorted(countryPicker)


override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.

// Hides the text field
questionTextField.hidden = true

questionTextField.delegate = self

// Sets the button text
buttonLabel.setTitle("Get started", forState: UIControlState.Normal)

// Sets the question text to be blank
questionLabel.text = ""

// Sets placeholder text in the text field
questionTextField.placeholder = ""

var pickerView = UIPickerView()

pickerView.delegate = self

questionTextField.inputView = pickerView


}

func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}

func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return countryPicker.count
}

func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
return countryPicker[row]
}

func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
questionTextField.text = countryPicker[row]
}



// resigns the keyboard when user presses the return/next key on keyboard

func textFieldShouldReturn(textField: UITextField) -> Bool {

questionTextField.resignFirstResponder()

return true

}

// Resigns the keyboard if the user presses anywhere on the screen
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {

self.view.endEditing(true)
}


override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

}

Upvotes: 0

Views: 1734

Answers (2)

Nick89
Nick89

Reputation: 2998

I managed to get was i was looking for with this:

countryPicker.sort(){$0 < $1}

It sorted the array from A-Z, and was the only way it worked for me.

Upvotes: 0

Duncan C
Duncan C

Reputation: 131481

Sort the array, or create a sorted version of the array, and then use that sorted array as the source for your picker view.

let array = ["orange", "grape", "apple", "pear", "mango"]
let sortedArray = sorted(array)

you would then use sortedArray in the data source for your picker view.

Upvotes: 3

Related Questions