3

I want to print the words in lexicographic order. I thought sorted() arranges the words in this way. I have also tried .sort() which returns the same order. Or am I missing something with what lexicographic order really is?

Code:

a_list = ['Zrhregegrydb', 'cygzRFWDWBdvF']
for word in sorted(a_list):
    print(word)

Output:

# Zrhregegrydb
# cygzRFWDWBdvF

Desired Output:

# cygzRFWDWBdvF
# Zrhregegrydb
Mureinik
  • 297,002
  • 52
  • 306
  • 350
Wizard
  • 1,533
  • 4
  • 19
  • 32
  • 1
    Do you want to sort lower-case letters before upper-case letters, or completely ignore the case? – Aran-Fey May 20 '18 at 16:46
  • Lexicographic order and case-insensitivity are seemingly independent but are in fact conflicting concepts. Letter casing is locale/culture-specific. Once you go there, you lose the concept of lexicographic order. Do you actually want a sort order based on the user's locale? – Tom Blodget May 20 '18 at 21:07

5 Answers5

5

By default, python already sorts strings in lexicographical order, but uppercase letters are all sorted before lowercase letters. If you want to sort strings and ignore case, then you can do

b_list = sorted(a_list, key=str.lower)
Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
  • specifying `str.lower` directly as the key is faster than calling a lambda which calls `str.lower`. Even better, use [`str.casefold`](https://docs.python.org/3/library/stdtypes.html#str.casefold). – PM 2Ring May 20 '18 at 16:57
  • @PM2Ring Good point. It's less typing, too. – Code-Apprentice May 20 '18 at 17:00
4

This is because the ASCII value of upper-case letters is smaller than lower-case. If you want your sort to ignore case you can do this, for example:

for word in sorted(a_list, key=str.lower):
Tané Tachyon
  • 1,092
  • 8
  • 11
  • The problem here is that lower- and uppercase letters will be in arbitrary order. Example outputs: `['a', 'A', 'b', 'B']` or `['a', 'A', 'B', 'b']` (depends on input order). – C. Yduqoli Jul 12 '19 at 02:27
3

You are missing something. In the standard, Unicode order (starting with the old ASCII order), all upper-case English letters come before all lower-case letters. You can see that in a table of Unicode characters.

If you want to ignore the case while sorting, as it seems you do, just temporarily convert all the letters to upper-case or to lower-case during the sort. Python allows you to do that in the sorted function with the key parameter, as you can see in other answers here (finished before I finished this answer).

Rory Daulton
  • 21,934
  • 6
  • 42
  • 50
2

You could ignore the case when sorting:

sorted_list = sorted(a_list, key = lambda s : s.lower())
Mureinik
  • 297,002
  • 52
  • 306
  • 350
-3

You have to iterate using the indexes to see the right order.

Here is the code how you can do it.

a_list = ['Zrhregegrydb', 'cygzRFWDWBdvF']
a_list.sort(reverse=True)
for i in range(0, len(a_list)):
    print a_list[i]
Usama Jamil
  • 68
  • 10
  • And the loop from the OP is preferred to looping over `range()`. – Code-Apprentice May 20 '18 at 16:44
  • I did and it worked perfectly I tested the code then given the answer can you check again – Usama Jamil May 20 '18 at 16:46
  • 1
    https://repl.it/@codeguru/TriangularNutritiousLicenses I get the output as `xyz` first then `abc` which is in reverse alphabetical order. This is not the OP's desired result. – Code-Apprentice May 20 '18 at 16:49
  • If you want abc first just remove reverse=True and it will do the job I provided the code according to his requirement – Usama Jamil May 20 '18 at 19:39
  • Yes, your code works for the example given. However, your proposed solution does not work for other possible inputs. Removing `reverse=True` just gives the original code from the question which is not a valid solution, either. – Code-Apprentice May 20 '18 at 19:44