How to Reverse Sort a nested list starting with Uppercase entries?
up vote
8
down vote
favorite
List r:
r= [['Paris', 10], ['amsterdam', 5], ['London', 18], ['london', 15], ['Berlin', 2], ['Stockholm', 4], ['oslo', 14], ['helsinki', 16], ['Zurich', 17]]
If I do a reverse sort:
sorted(r, reverse=True)
[['oslo', 14], ['london', 15], ['helsinki', 16], ['amsterdam', 5], ['Zurich', 17], ['Stockholm', 4], ['Paris', 10], ['London', 18], ['Berlin', 2]]
What I want is to start with Upper case elements and than the lower case elements.
[ ['Zurich', 17], ['Stockholm', 4], ['Paris', 10], ['London', 18], ['Berlin', 2], ['oslo', 14], ['london', 15], ['helsinki', 16], ['amsterdam', 5]]
Is there an easy way in Python3 to sort the list as I want?
python list sorting case
add a comment |
up vote
8
down vote
favorite
List r:
r= [['Paris', 10], ['amsterdam', 5], ['London', 18], ['london', 15], ['Berlin', 2], ['Stockholm', 4], ['oslo', 14], ['helsinki', 16], ['Zurich', 17]]
If I do a reverse sort:
sorted(r, reverse=True)
[['oslo', 14], ['london', 15], ['helsinki', 16], ['amsterdam', 5], ['Zurich', 17], ['Stockholm', 4], ['Paris', 10], ['London', 18], ['Berlin', 2]]
What I want is to start with Upper case elements and than the lower case elements.
[ ['Zurich', 17], ['Stockholm', 4], ['Paris', 10], ['London', 18], ['Berlin', 2], ['oslo', 14], ['london', 15], ['helsinki', 16], ['amsterdam', 5]]
Is there an easy way in Python3 to sort the list as I want?
python list sorting case
I think the behaviour is because of the ASCII values of the capital and small letters.
– Nitin Pawar
Dec 3 at 11:03
1
First sort by name, then by "casing":r.sort(key=lambda x: x[0]); r.sort(key=lambda x: x[0].islower()). This works becauselist.sortuses a stable sorting algorithm (i.e. it preserves the relative order of equal elements).list.sortis also an adaptive sort, which means it can take O(n) time if the input is partially sorted and after the first sort the input is very well partially sorted according to the keyx[0][0].islower().
– Giacomo Alzetta
Dec 3 at 13:34
add a comment |
up vote
8
down vote
favorite
up vote
8
down vote
favorite
List r:
r= [['Paris', 10], ['amsterdam', 5], ['London', 18], ['london', 15], ['Berlin', 2], ['Stockholm', 4], ['oslo', 14], ['helsinki', 16], ['Zurich', 17]]
If I do a reverse sort:
sorted(r, reverse=True)
[['oslo', 14], ['london', 15], ['helsinki', 16], ['amsterdam', 5], ['Zurich', 17], ['Stockholm', 4], ['Paris', 10], ['London', 18], ['Berlin', 2]]
What I want is to start with Upper case elements and than the lower case elements.
[ ['Zurich', 17], ['Stockholm', 4], ['Paris', 10], ['London', 18], ['Berlin', 2], ['oslo', 14], ['london', 15], ['helsinki', 16], ['amsterdam', 5]]
Is there an easy way in Python3 to sort the list as I want?
python list sorting case
List r:
r= [['Paris', 10], ['amsterdam', 5], ['London', 18], ['london', 15], ['Berlin', 2], ['Stockholm', 4], ['oslo', 14], ['helsinki', 16], ['Zurich', 17]]
If I do a reverse sort:
sorted(r, reverse=True)
[['oslo', 14], ['london', 15], ['helsinki', 16], ['amsterdam', 5], ['Zurich', 17], ['Stockholm', 4], ['Paris', 10], ['London', 18], ['Berlin', 2]]
What I want is to start with Upper case elements and than the lower case elements.
[ ['Zurich', 17], ['Stockholm', 4], ['Paris', 10], ['London', 18], ['Berlin', 2], ['oslo', 14], ['london', 15], ['helsinki', 16], ['amsterdam', 5]]
Is there an easy way in Python3 to sort the list as I want?
python list sorting case
python list sorting case
asked Dec 3 at 11:01
Reman
3,48243572
3,48243572
I think the behaviour is because of the ASCII values of the capital and small letters.
– Nitin Pawar
Dec 3 at 11:03
1
First sort by name, then by "casing":r.sort(key=lambda x: x[0]); r.sort(key=lambda x: x[0].islower()). This works becauselist.sortuses a stable sorting algorithm (i.e. it preserves the relative order of equal elements).list.sortis also an adaptive sort, which means it can take O(n) time if the input is partially sorted and after the first sort the input is very well partially sorted according to the keyx[0][0].islower().
– Giacomo Alzetta
Dec 3 at 13:34
add a comment |
I think the behaviour is because of the ASCII values of the capital and small letters.
– Nitin Pawar
Dec 3 at 11:03
1
First sort by name, then by "casing":r.sort(key=lambda x: x[0]); r.sort(key=lambda x: x[0].islower()). This works becauselist.sortuses a stable sorting algorithm (i.e. it preserves the relative order of equal elements).list.sortis also an adaptive sort, which means it can take O(n) time if the input is partially sorted and after the first sort the input is very well partially sorted according to the keyx[0][0].islower().
– Giacomo Alzetta
Dec 3 at 13:34
I think the behaviour is because of the ASCII values of the capital and small letters.
– Nitin Pawar
Dec 3 at 11:03
I think the behaviour is because of the ASCII values of the capital and small letters.
– Nitin Pawar
Dec 3 at 11:03
1
1
First sort by name, then by "casing":
r.sort(key=lambda x: x[0]); r.sort(key=lambda x: x[0].islower()). This works because list.sort uses a stable sorting algorithm (i.e. it preserves the relative order of equal elements). list.sort is also an adaptive sort, which means it can take O(n) time if the input is partially sorted and after the first sort the input is very well partially sorted according to the key x[0][0].islower().– Giacomo Alzetta
Dec 3 at 13:34
First sort by name, then by "casing":
r.sort(key=lambda x: x[0]); r.sort(key=lambda x: x[0].islower()). This works because list.sort uses a stable sorting algorithm (i.e. it preserves the relative order of equal elements). list.sort is also an adaptive sort, which means it can take O(n) time if the input is partially sorted and after the first sort the input is very well partially sorted according to the key x[0][0].islower().– Giacomo Alzetta
Dec 3 at 13:34
add a comment |
1 Answer
1
active
oldest
votes
up vote
16
down vote
accepted
Everything is possible with a custom key function:
>> sorted(r, key=lambda e: (not e[0].islower(), e[0]), reverse=True)
[['Zurich', 17], ['Stockholm', 4], ['Paris', 10], ['London', 18], ['Berlin', 2],
['oslo', 14], ['london', 15], ['helsinki', 16], ['amsterdam', 5]]
1
Great solution. Thanks. It also works the other way aroundsorted(r, key=lambda e: (not e[0].islower(), e[0]))(=default case sort but starting with lowercase entries).
– Reman
Dec 3 at 11:28
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
16
down vote
accepted
Everything is possible with a custom key function:
>> sorted(r, key=lambda e: (not e[0].islower(), e[0]), reverse=True)
[['Zurich', 17], ['Stockholm', 4], ['Paris', 10], ['London', 18], ['Berlin', 2],
['oslo', 14], ['london', 15], ['helsinki', 16], ['amsterdam', 5]]
1
Great solution. Thanks. It also works the other way aroundsorted(r, key=lambda e: (not e[0].islower(), e[0]))(=default case sort but starting with lowercase entries).
– Reman
Dec 3 at 11:28
add a comment |
up vote
16
down vote
accepted
Everything is possible with a custom key function:
>> sorted(r, key=lambda e: (not e[0].islower(), e[0]), reverse=True)
[['Zurich', 17], ['Stockholm', 4], ['Paris', 10], ['London', 18], ['Berlin', 2],
['oslo', 14], ['london', 15], ['helsinki', 16], ['amsterdam', 5]]
1
Great solution. Thanks. It also works the other way aroundsorted(r, key=lambda e: (not e[0].islower(), e[0]))(=default case sort but starting with lowercase entries).
– Reman
Dec 3 at 11:28
add a comment |
up vote
16
down vote
accepted
up vote
16
down vote
accepted
Everything is possible with a custom key function:
>> sorted(r, key=lambda e: (not e[0].islower(), e[0]), reverse=True)
[['Zurich', 17], ['Stockholm', 4], ['Paris', 10], ['London', 18], ['Berlin', 2],
['oslo', 14], ['london', 15], ['helsinki', 16], ['amsterdam', 5]]
Everything is possible with a custom key function:
>> sorted(r, key=lambda e: (not e[0].islower(), e[0]), reverse=True)
[['Zurich', 17], ['Stockholm', 4], ['Paris', 10], ['London', 18], ['Berlin', 2],
['oslo', 14], ['london', 15], ['helsinki', 16], ['amsterdam', 5]]
answered Dec 3 at 11:06
DeepSpace
35.4k44067
35.4k44067
1
Great solution. Thanks. It also works the other way aroundsorted(r, key=lambda e: (not e[0].islower(), e[0]))(=default case sort but starting with lowercase entries).
– Reman
Dec 3 at 11:28
add a comment |
1
Great solution. Thanks. It also works the other way aroundsorted(r, key=lambda e: (not e[0].islower(), e[0]))(=default case sort but starting with lowercase entries).
– Reman
Dec 3 at 11:28
1
1
Great solution. Thanks. It also works the other way around
sorted(r, key=lambda e: (not e[0].islower(), e[0])) (=default case sort but starting with lowercase entries).– Reman
Dec 3 at 11:28
Great solution. Thanks. It also works the other way around
sorted(r, key=lambda e: (not e[0].islower(), e[0])) (=default case sort but starting with lowercase entries).– Reman
Dec 3 at 11:28
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53592424%2fhow-to-reverse-sort-a-nested-list-starting-with-uppercase-entries%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
I think the behaviour is because of the ASCII values of the capital and small letters.
– Nitin Pawar
Dec 3 at 11:03
1
First sort by name, then by "casing":
r.sort(key=lambda x: x[0]); r.sort(key=lambda x: x[0].islower()). This works becauselist.sortuses a stable sorting algorithm (i.e. it preserves the relative order of equal elements).list.sortis also an adaptive sort, which means it can take O(n) time if the input is partially sorted and after the first sort the input is very well partially sorted according to the keyx[0][0].islower().– Giacomo Alzetta
Dec 3 at 13:34