Why would it be an advantage to use numbers as sort keys instead of sorting the strings directly?

computer science

Description

 The programming language we use is Python, although that might not be so releavnt for these questions.


We often need to sort collections of strings, for examples names in a phonebook. Advanced sorting algorithms will often convert all the strings to numbers first (each string to a different number) and then sort by comparing those numbers (known as sort keys).


Why would it be an advantage to use numbers as sort keys instead of sorting the strings directly?


Consider the method used to convert strings to numbers: which condition must the numbers created fulfill?


Can you suggest an algorithm for converting strings into numbers to be used as sort keys? Assume that each letter in a string is 1 byte long, that numbers are 8 bytes long and that all strings are at most 8 letters long.


As an additional challenge: How could you extend the approach to strings of any length?


Related Questions in computer science category