Swift Challenge 005: Sort comma separated wordsPosted: 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(",") print(br.sort())
- string separation
- array sorting
Got comments? Tweet it, or comment below.