Code Lab

Remove the spaces and sort

MediumStrings10 min

Write a function sortCharacters(text) that removes all whitespace from the string text and returns the other characters sorted.

  • text has only ASCII characters: letters, digits, punctuation and whitespace.
  • Whitespace is a space, a tab \t and a line break \n or \r. All of it goes, not only spaces.
  • Each character is sorted by its character code, from small to large. This is the order of sort without a compare function. For example, ! and , come before the digits, the digits come before the capital letters, and the capital letters come before the small letters. So 'Z' comes before 'a'.
  • Repeated characters stay: 'banana' gives 'aaabnn'.
  • Return a string. An empty string or a string of only whitespace gives ''.

Examples

sortCharacters('My Name is Nikhil')

Returns 'MNNaehiiiklmsy'

sortCharacters('Zebra 123')

Returns '123Zaber'

Asked at Cognizant · Source

Loading the editor...
  1. InputsortCharacters('My Name is Nikhil')
    Expected'MNNaehiiiklmsy'
  2. InputsortCharacters('b a')
    Expected'ab'

Hidden tests run on Submit.