Common Issues With Sorting in Python
Some types of data can’t be sorted because they are too different to be compared. Let’s take a look at some in the interpreter. Go ahead and make a list called
mixed_types and set this equal to
[None, 0]. If you call
mixed_types, you’ll get an error.
This is because
sorted() is trying to do a less than comparison between an integer and a
NoneType. You can actually take a look at this and see it for yourself if you do
None < 0, and you’ll get that same issue there.
If you have multiple data types in your list and they can be compared without throwing a
TypeError, then you should be okay. Sometimes you’re able to convert your elements so they can be compared in a process known as casting.
you’ll see that you get this error here, because you’re trying to compare strings and integers. But you know that this
"1" can be evaluated as an integer if you were to call something like
int() and pass in the
Sometimes, Python will try to implicitly convert values to different data types as needed. You might be familiar with something like this if you ever do an
if statement and pass in an empty list, because you know that an empty list will evaluate to
Right away, Python is going to try to evaluate those comparisons. So if you just take a look at
similar_values, you’re going to end up with
[False, 0, 1, False, False] because these aren’t equal and
1 is greater than
So let’s call
similar_values. And look at that! You’ve got
False, and then
1 has been brought to the end. Now, interestingly, the
0 was not sorted separately from these
False values, so you can tell that Python was evaluating these to be equal.
This is because Python is using Unicode code points to return a number value for each letter. To see this, let’s go ahead and make a
names_with_case list, which is going to equal
'harry', and then
'Suzy' with a capital
you’ll see that the capital letters appear first. And this makes sense if you were to look at the Unicode code points for each of those letters. Let’s go ahead and make a new list here, and you’re going to use the
ord() function for the first letter in each name, and then also just show what that first letter is in each name.
You’ll see here that the capital letters have lower
ord() values than lowercase letters, and they’re sorted in ascending order. Now, we haven’t looked at any strings where they both start with the same letter, but if this is the case, Python will just look at the next letter and continue on until it finds a difference.
05:54 Hopefully you have a pretty good idea of how to deal with some of the strange cases that can come up when you’re sorting. The big takeaways are to keep in mind that everything in your list or iterable that you’re trying to sort must be comparable.
And when you’re trying to sort alphabetically, you’re actually sorting off of the Unicode code point value. In the next video, we’re going to talk about times when you’ll want to use
.sort() and vice versa.
Become a Member to join the conversation.