2

This is a fun little challenge that confronted me recently. I'll provide my answer below, but I'm curious to see whether there are more elegant or efficient solutions.

A delineation of the requirements as they were presented to me:

  1. Strings are alphanumeric (see test dataset below)
  2. Strings should be sorted naturally (see this question for explanation)
  3. Alpha characters should be sorted ahead of numeric characters (i.e. 'abc' before '100')
  4. Uppercase instances of alpha chars should be sorted ahead of lowercase instances (i.e. 'ABc', 'Abc', 'abc')

Here's a test dataset:

test_cases = [
    # (unsorted list, sorted list)
    (list('bca'), ['a', 'b', 'c']),
    (list('CbA'), ['A', 'b', 'C']),
    (list('r0B9a'), ['a', 'B', 'r', '0', '9']),
    (['a2', '1a', '10a', 'a1', 'a100'], ['a1', 'a2', 'a100', '1a', '10a']),
    (['GAM', 'alp2', 'ALP11', '1', 'alp100', 'alp10', '100', 'alp1', '2'],
        ['alp1', 'alp2', 'alp10', 'ALP11', 'alp100', 'GAM', '1', '2', '100']),
    (list('ra0b9A'), ['A', 'a', 'b', 'r', '0', '9']),
    (['Abc', 'abc', 'ABc'], ['ABc', 'Abc', 'abc']),
]

Bonus Test Case

This is inspired by Janne Karila's comment below that the selected answer currently fails (but wouldn't really be a practical concern in my case):

(['0A', '00a', 'a', 'A', 'A0', '00A', '0', 'a0', '00', '0a'],
        ['A', 'a', 'A0', 'a0', '0', '00', '0A', '00A', '0a', '00a'])
Community
  • 1
  • 1
klenwell
  • 6,978
  • 4
  • 45
  • 84
  • Challenges like this, especially those that already come with a solution, typically belong on Code Review or Code Golf – David Robinson Aug 29 '12 at 18:22
  • Janne: Corrected title. Good catch. Thanks! // David: What's Code Review or Code Golf? And if that's true, what was the point of adding an "Answer Your Own Question" box to questions? – klenwell Aug 29 '12 at 18:25
  • `a100` should come before `a2` – Keith Randall Aug 29 '12 at 18:30
  • And by "Uppercase characters should be sorted before lowercase" do you mean `Z` before `a`? Your examples seem otherwise. – Keith Randall Aug 29 '12 at 18:32
  • 1
    @KeithRandall, by the rules of natural sorting the whole number should be considered, not just individual digits - so `a2` should come before `a100`. As for upper/lowercase order, I think the order is meant to be `AaBbCcDd...`. – Mark Ransom Aug 29 '12 at 18:44
  • here is a reference to natural sorting with python example http://www.codinghorror.com/blog/2007/12/sorting-for-humans-natural-sort-order.html – Michael Aug 29 '12 at 18:52
  • Your 5th test case is a good one, blew up my attempt right away. – Mark Ransom Aug 29 '12 at 19:20
  • Ah, I see. The reference in the question could be better, the link defines "natural sort" to be "the order in which Windows sorts", not very helpful. Michael's link helps a lot more. – Keith Randall Aug 29 '12 at 19:44
  • 1
    Which should come first: `00A` or `0a`? – Janne Karila Aug 29 '12 at 19:45
  • 1
    @JanneKarila, I don't have the "official" specs but I'll tell you how it works for my answer. The `00` and `0` compare equal, and the `A` and `a` compare equal also. Thus it falls to the tie-breaker, which is comparing the original strings in a binary fashion, and `00` comes before `0a`. – Mark Ransom Aug 30 '12 at 15:00
  • @MarkRansom So, in your answer `00A < 0a` and `00a < 0A`. If you removed digits from the tie-breaker, the one with `A` would come first in both cases. Then the case of letters would truly serve as the tie-breaker. – Janne Karila Aug 30 '12 at 15:42
  • @JanneKarila That's an interesting corner case that wasn't considered. The goal was to order the strings in the way that would make it easiest for a human being (actually, _the_ human being) glancing at them to quickly make sense of them. Here's what I would propose for the most intuitive ordering of the following list (given the other requirements): ['A', 'a', 'A0', 'a0', '0', '00', '0A', '00A', '0a', '00a']. The solutions below fail this expectation for the reason Mark explained. For my particular use case, however, this issue likely wouldn't come up. – klenwell Aug 30 '12 at 15:42

3 Answers3

8
re_natural = re.compile('[0-9]+|[^0-9]+')

def natural_key(s):
    return [(1, int(c)) if c.isdigit() else (0, c.lower()) for c in re_natural.findall(s)] + [s]

for case in test_cases:
    print case[1]
    print sorted(case[0], key=natural_key)

['a', 'b', 'c']
['a', 'b', 'c']
['A', 'b', 'C']
['A', 'b', 'C']
['a', 'B', 'r', '0', '9']
['a', 'B', 'r', '0', '9']
['a1', 'a2', 'a100', '1a', '10a']
['a1', 'a2', 'a100', '1a', '10a']
['alp1', 'alp2', 'alp10', 'ALP11', 'alp100', 'GAM', '1', '2', '100']
['alp1', 'alp2', 'alp10', 'ALP11', 'alp100', 'GAM', '1', '2', '100']
['A', 'a', 'b', 'r', '0', '9']
['A', 'a', 'b', 'r', '0', '9']
['ABc', 'Abc', 'abc']
['ABc', 'Abc', 'abc']

Edit: I decided to revisit this question and see if it would be possible to handle the bonus case. It requires being more sophisticated in the tie-breaker portion of the key. To match the desired results, the alpha parts of the key must be considered before the numeric parts. I also added a marker between the natural section of the key and the tie-breaker so that short keys always come before long ones.

def natural_key2(s):
    parts = re_natural.findall(s)
    natural = [(1, int(c)) if c.isdigit() else (0, c.lower()) for c in parts]
    ties_alpha = [c for c in parts if not c.isdigit()]
    ties_numeric = [c for c in parts if c.isdigit()]
    return natural + [(-1,)] + ties_alpha + ties_numeric

This generates identical results for the test cases above, plus the desired output for the bonus case:

['A', 'a', 'A0', 'a0', '0', '00', '0A', '00A', '0a', '00a']
Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
3

Here's one that works for the bonus test too:

def mykey(s):
    lst = re.findall(r'(\d+)|(\D+)', s)
    return [(0,a.lower()) if a else (1,int(n)) for n, a in lst]\
        + [a for n, a in lst if a]\
        + [len(n) for n, a in lst if n]

def mysort(lst):
    return sorted(lst, key=mykey)

With this type of pattern, re.findall breaks the string to a list of tuples, e.g.

>>> re.findall(r'(\d+)|(\D+)', 'ab12cd')
[('', 'ab'), ('12', ''), ('', 'cd')]
Janne Karila
  • 24,266
  • 6
  • 53
  • 94
  • I like the way you used the regular expression to simplify the comprehension conditionals. – Mark Ransom Dec 13 '12 at 19:07
  • P.S. This relies on Python's ordering guarantees between tuples and non-tuples, which doesn't work at all in Python 3. You could fix it the same way I did by putting a dummy marker before the tie-breakers. See http://ideone.com/ibxLi0 – Mark Ransom Dec 13 '12 at 20:18
0

This function makes no claims as to performance at this time:

def alpha_before_numeric_natural_sensitive(unsorted_list):
    """presorting the list should work because python stable sorts; see:
    http://wiki.python.org/moin/HowTo/Sorting/#Sort_Stability_and_Complex_Sorts"""
    presorted_list = sorted(unsorted_list)
    return alpha_before_numeric_natural(presorted_list)

def alpha_before_numeric_natural(unsorted_list):
    """splice each string into tuple like so:
    'abc100def' -> ('a', 'b', 'c', 100, 'd', 'e', 'f') ->
    (ord('a'), ord('b'), ord('c'), ord('z') + 1 + 100, ...) then compare
    each tuple"""
    re_p = "([0-9]+|[A-za-z])"
    ordify = lambda s: ord('z') + 1 + int(s) if s.isdigit() else ord(s.lower())
    str_to_ord_tuple = lambda key: [ordify(c) for c in re.split(re_p, key) if c]
    return sorted(unsorted_list, key=str_to_ord_tuple)

It's based off the insight provided by this natural sort solution and this function that I wrote:

def alpha_before_numeric(unsorted_list):
    ord_shift = lambda c: c.isdigit() and chr(ord('z') + int(c.lower()) + 1) or c.lower()
    adjust_word = lambda word: "".join([ord_shift(c) for c in list(word)])

    def cmp_(a, b):
        return cmp(adjust_word(a), adjust_word(b))

    return sorted(unsorted_list, cmp_)

For full test script here that compares different functions, see http://klenwell.com/is/Pastebin20120829

Community
  • 1
  • 1
klenwell
  • 6,978
  • 4
  • 45
  • 84