Skip to content
Advertisement

How to convert a char array to a number for sorting?

Let’s say I have three words I want to sort:

  • hello
  • goodbye
  • alex

And alphabetically it’d be sorted ascendingly as ["alex", "goodbye", "hello"]. Is there a way to convert the string (limited to 100 characters)? For example, if I have:

['a', 'l', 'e', 'x']

And I use ord to get the code for each list element:

[1, 12, 5, 24]

How would I create a number from that, that would also be smaller than, say, goodbye?

Advertisement

Answer

Use:

from operator import mul, pow
from itertools import repeat

LIMIT = 100
char_vals = [1, 12, 5, 24]
sum(map(mul, char_vals, map(pow, repeat(27), range(LIMIT - 1, -1, -1))))


Since you’ve bounded the length of the strings, you can think of each word as a base-27 number. 0 represents that the letter doesn’t exist, 1 represents a, 2 represents b, etc.

Then, the numerical value of each word can be computed using the following polynomial:

i_1 * 27**99 + i_2 * 27**98 + ...

where char_vals = [i_1, i_2, ... i_{len(original_string)}] (i.e. each i is the integer value of the corresponding letter, and is that are past the end of the array correspond to zeroes).

Advertisement