Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
lists.sort() with cmp
#2
cmp is is removed in Python 3.
Quote:The cmp() function should be treated as gone, and the __cmp__() special method is no longer supported.
So it was a bad idea,and key=somthing is better and works also fine in Python 2.7.
Quote:and I would prefer a way without lambda, if that's possible. I seem to remember that a function could be used?
You can import itemgetter to avoid lambda.
But lambda is really nice with sorting:
>>> lst = [('Tom', 'C', 10), ('Kent', 'A', 12), ('Jenny', 'B', 15)]
>>> sorted(lst, key=lambda tup: tup[1])
[('Kent', 'A', 12), ('Jenny', 'B', 15), ('Tom', 'C', 10)]
Sorting middle elements to A,B,C.
Sorting HOW TO
Reply


Messages In This Thread
lists.sort() with cmp - by merlem - Feb-06-2017, 04:47 PM
RE: lists.sort() with cmp - by snippsat - Feb-06-2017, 05:26 PM
RE: lists.sort() with cmp - by merlem - Feb-06-2017, 06:01 PM
RE: lists.sort() with cmp - by snippsat - Feb-06-2017, 06:14 PM
RE: lists.sort() with cmp - by merlem - Feb-06-2017, 08:09 PM
RE: lists.sort() with cmp - by Ofnuts - Feb-06-2017, 08:32 PM
RE: lists.sort() with cmp - by Ofnuts - Feb-06-2017, 06:40 PM
RE: lists.sort() with cmp - by wavic - Feb-06-2017, 06:41 PM
RE: lists.sort() with cmp - by snippsat - Feb-06-2017, 07:14 PM
RE: lists.sort() with cmp - by nilamo - Feb-08-2017, 07:29 AM
RE: lists.sort() with cmp - by merlem - Feb-09-2017, 02:05 PM
RE: lists.sort() with cmp - by wavic - Feb-09-2017, 02:16 PM
RE: lists.sort() with cmp - by merlem - Feb-09-2017, 03:29 PM
RE: lists.sort() with cmp - by Ofnuts - Feb-09-2017, 09:12 PM
RE: lists.sort() with cmp - by snippsat - Feb-09-2017, 09:42 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
Photo a.sort() == b.sort() all the time 3lnyn0 1 1,371 Apr-19-2022, 06:50 PM
Last Post: Gribouillis
  Sort List of Lists by Column Nju 1 12,940 Apr-13-2021, 11:59 PM
Last Post: bowlofred
  sort lists of lists with multiple criteria: similar values need to be treated equal stillsen 2 3,363 Mar-20-2019, 08:01 PM
Last Post: stillsen
  Creating new list from 2 lists after a custom sort pythoneer 12 6,201 Jun-01-2018, 04:55 PM
Last Post: pythoneer

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020