1

I have a list of filenames conforming to the pattern: s[num][alpha1][alpha2].ext

I need to sort, first by the number, then by alpha1, then by alpha2. The last two aren't alphabetical, however, but rather should reflect a custom ordering.

I've created two lists representing the ordering for alpha1 and alpha2, like so:

alpha1Order = ["Fizz", "Buzz", "Ipsum", "Dolor", "Lorem"]
alpha2Order = ["Sit", "Amet", "Test"]

What's the best way to proceed? My first though was to tokenize (somehow) such that I split each filename into its component parts (s, num, alpha1, alpha2), then sort, but I wasn't quite sure how to perform such a complicated sort. Using a key function seemed clunky, as this sort didn't seem to lend itself to a simple ordering.

2
  • Once tokenized, your data is perfectly orderable with a key function. Commented Jan 14, 2014 at 17:14
  • Could you point me to more information on how to use the key function to order by multiple attributes? I wasn't sure how to order by one first, then deeper by the second, then deeper by the third. Commented Jan 14, 2014 at 17:15

1 Answer 1

3

Once tokenized, your data is perfectly orderable with a key function. Just return the index of the alpha1Order and alpha2Order lists for the value. Replace them with dictionaries to make the lookup easier:

alpha1Order = {token: i for i, token in enumerate(alpha1Order)}
alpha2Order = {token: i for i, token in enumerate(alpha2Order)}

def keyfunction(filename):
    num, alpha1, alpha2 = tokenize(filename)
    return int(num), alpha1Order[alpha1], alpha2Order[alpha2]

This returns a tuple to sort on; Python will use the first value to sort on, ordering anything that has the same int(num) value by the second entry, using the 3rd to break any values tied on the first 2 entries.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, Martijn. I've almost solved this problem, but ran into further issues with the tokenization. I've posted that as a separate question due to its complexity, and would like to solve both issues before I resolve. stackoverflow.com/questions/21120378/…
That's a separate issue; this answer doesn't stand or fall with how you solve the tokenization.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.