-3

What's the default order for sorting user-defined objects in Python? I know how to change this, using the key parameter or defining the __lt__ etc, methods, but not what order you get if you don't.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
xorsyst
  • 7,897
  • 5
  • 39
  • 58
  • You should probably start by reading the library manual before asking questions: https://docs.python.org/2/library/stdtypes.html#mutable-sequence-types – skyking Oct 27 '15 at 10:45
  • Sorting *what* objects? – jonrsharpe Oct 27 '15 at 10:49
  • I'm not sure how that answers the question. And please don't make assumptions of laziness, it is rude. I spent quite a bit of time researching this before asking. – xorsyst Oct 27 '15 at 10:49
  • Any objects of classes you defined yourself. – xorsyst Oct 27 '15 at 10:49
  • 1. If you don't want people to assume you did no research, *show what research you did*. 2. An example always helps (define an object, put a few in a list, sort it, ...). 3. Which version(s) of Python? – jonrsharpe Oct 27 '15 at 10:51
  • 2
    Objects of different types, except different numeric types and different string types, never compare equal; such objects are ordered consistently but arbitrarily (so that sorting a heterogeneous array yields a consistent result). ... CPython implementation detail: Objects of different types except numbers are ordered by their type names; objects of the same types that don’t support proper comparison are ordered by their address. https://docs.python.org/2/library/stdtypes.html#comparisons – khelwood Oct 27 '15 at 10:52
  • Thanks @khelwood, if you put that as an answer I'd accept it. – xorsyst Oct 27 '15 at 11:07

1 Answers1

-3

Python sorts objects by default in ascending order. A simple ascending sort is done by calling the sorted() function.

It returns a new sorted list.

Henin RK
  • 298
  • 1
  • 2
  • 14
  • 2
    But how is _ascending_ defined for classes that don't have comparison methods? – xorsyst Oct 27 '15 at 10:50
  • 1
    @xorsyst why on earth have you accepted this answer? You pointed out *yourself* that it doesn't actually answer the question! – jonrsharpe Oct 27 '15 at 13:43