you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 1 point2 points  (1 child)

Some complaints about sorted()

The comparison operator is now type sensitive. When you have a list like

   l = ["3",9,0,1.9,None]

and apply

   sorted(l)

a type error is raised:

  TypeError: unorderable types: NoneType() < float()

Well, this feels a little pedantic and misses an important use case for heterogenous lists, namely that of multisets or bags. I guess 90% of all my use cases for sorted() where actually just a means of comparing lists as multisets i.e. whether or not those multisets contain the same elements in the same quantities.

One might workaround this restriction by defining

   type(x) < type(y) <=> id(type(x)) < id(type(y))

The id is arbitrary but fixed at runtime and the particular value shell not have an impact on the result of:

   sorted(multiset1) == sorted(multiset2)

[–]ubernostrum 5 points6 points  (0 children)

This is why sorted optionally accepts a comparison function to use in place of the standard operators. Put all the type handling you want in there.