Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
sorting
#21
The lambda would look like this as a full function:

def sort_order(item):
    if isinstance(item, (int, float)):
        index = 0
    else:
        index = 1
    return (index, item)
You could simplify that some, but I'm trying to be clear. As zivoni points out, you can expand that:


def sort_order(item):
    if isinstance(item, (int, float)):
        index = 0
    elif isinstance(item, str):
        index = 1
    elif isinstance(item, (tuple, list)):
        index = 2
    elif isinstance(item, (dict, set)):
        index = 3
    else:
        index = 99
    return (index, item)
And of course, any big if/elif/else structure suggests a dictionary:

type_order = {int: 0, float: 0, str: 1, list: 2, tuple: 2, dict: 3, set: 3}
mixed_list.sort(key = lambda x: (type_order.get(x, 99), x))
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Messages In This Thread
sorting - by Skaperen - Mar-15-2017, 04:04 AM
RE: sorting - by wavic - Mar-15-2017, 04:40 AM
RE: sorting - by Skaperen - Mar-15-2017, 05:04 AM
RE: sorting - by wavic - Mar-15-2017, 05:35 AM
RE: sorting - by zivoni - Mar-15-2017, 07:15 AM
RE: sorting - by Skaperen - Mar-15-2017, 09:04 AM
RE: sorting - by zivoni - Mar-15-2017, 09:56 AM
RE: sorting - by wavic - Mar-15-2017, 11:10 AM
RE: sorting - by zivoni - Mar-15-2017, 11:52 AM
RE: sorting - by wavic - Mar-15-2017, 01:06 PM
RE: sorting - by Skaperen - Mar-16-2017, 02:04 AM
RE: sorting - by ichabod801 - Mar-17-2017, 12:24 AM
RE: sorting - by Skaperen - Mar-17-2017, 02:39 AM
RE: sorting - by wavic - Mar-17-2017, 07:41 AM
RE: sorting - by zivoni - Mar-17-2017, 09:29 AM
RE: sorting - by wavic - Mar-17-2017, 09:54 AM
RE: sorting - by zivoni - Mar-17-2017, 10:18 AM
RE: sorting - by Skaperen - Mar-18-2017, 02:46 AM
RE: sorting - by zivoni - Mar-18-2017, 10:16 AM
RE: sorting - by wavic - Mar-18-2017, 10:23 AM
RE: sorting - by Ofnuts - Mar-18-2017, 10:57 PM
RE: sorting - by ichabod801 - Mar-18-2017, 10:37 AM
RE: sorting - by zivoni - Mar-18-2017, 11:20 AM
RE: sorting - by wavic - Mar-18-2017, 11:38 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Sorting a copied list is also sorting the original list ? SN_YAZER 3 3,270 Apr-11-2019, 05:10 PM
Last Post: SN_YAZER

Forum Jump:

User Panel Messages

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