Joseph Jude

Technology, Psychology, and Story Telling

Swift Challenge 005: Sort comma separated words

Posted: Tags: code,swift

Challenge: Write a program that takes a comma separated sequence of words and prints the words in a comma-separated sequence after sorting them alphabetically. Suppose the following input is supplied to the program: without,hello,bag,world. Then, the output should be: bag,hello,without,world

Logic: Separate the words and then sort them


import Foundation
let ar = "without,hello,bag,world"
let br = ar.componentsSeparatedByString(",")


  • string separation
  • array sorting

Git Repository / All Swift Challenges

Got comments? Tweet it, or comment below.


comments powered by Disqus