1

Consider two lists:

list1=[-4,-5,-3]
list2=['-4','-5','-3']

Now if we use

list1.sort()
list2.sort() # in python3 

We get contradictory results:

[-5, -4, -3]
['-3', '-4', '-5']`

Why is it so and how can we do it right!?

2
  • 1
    This is because in one case you are sorting integer in the others case you are sorting string. please be more specific on whet you call right so we can help you. Commented Mar 6, 2019 at 10:08
  • The second list sorts lexicographically, which in short means, that the elements are sorted "letter-wise" when containing the same suffix. That ist why "ab" comes after "aa" and "-4" comes after "-3". Commented Mar 6, 2019 at 10:09

4 Answers 4

8

The list1 sort is pretty self explanatory as it is just sorting the numbers numerically.

In list2 the values are stored as strings. So it is comparing and sorting them by the ASCII value. The digit 3 has an ASCII value of 51, 4 has a value of 52 and 5 has a value of 53. So it is working completely correctly, if you want to sort out words this is the way you want to do it.

However if you are just wanting to sort digits in the correct order make sure they are ints like list1. Or you can set the key in the sort method to cast them as ints so it is sorted in the numerical way like this:

list2.sort(key=int)
Sign up to request clarification or add additional context in comments.

3 Comments

Or, if you want to sort strings that represent integers like they would be sorted if they were integers, you can add the key parameter to .sort(): .sort(key=int). This will implicitly cast them to integers when sorting without modifying the list itself.
... and the '-' are all ignored since they are the same in each string.You can force python to compare the strings as integers by using key: list2.sort(key=int)
I've edited my answer, that's a pretty decent way of doing it.
2

The elements in list [-4,-5,-3] are numbers whereas elements in list ['-4','-5','-3'] are strings(because the numbers in the list are between 'single_qoutes').

So, the reason for getting contradictory results is that when you sort numbers, you get back [-5, -4, -3], which is sorted by there numerical value.

When you sort the other list with strings, it sorts it by alphabetically wherein 3, 4 and 5 would be correct way ('-' is the first character and 3, 4 and 5 are the characters after it.) to sort it based on its ASCII value.

So if you want to sort integers, don't include them between quotes.

Comments

1
  • Why is it so?

You can check that '-3' < '-4'. String compasion checks first symbols '-' == '-', check second symbol '3' < '4', so '-3' < '-4'.

  • How we can do it right?

It depends on what you call right. If you want to sort integers, Python does it right. If you want to sort string, Python does it right too.

Comments

0

Well this is two arrays with different contents. List1 is an array with numbers, List2 is an array of strings. That's why they don´t sort the same.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.