← Back to challenges

Sort a String by Its Last Character

PythonHardstringssorting

Instructions

Create a function that takes a string of words and return a string sorted alphabetically by the last character of each word.

Examples

sort_by_last("herb camera dynamic") ➞ "camera herb dynamic"

sort_by_last("stab traction artist approach") ➞ "stab approach traction artist"

sort_by_last("sample partner autonomy swallow trend") ➞ "trend sample partner swallow autonomy"

Notes

  • Tests consist of lowercase alphabetic characters (a-z) and spaces.
  • If two words have the same last character, sort by the order they originally appeared.
python3
Loading editor…
to run
Walks through the solution with reasoning and edge cases.