# Python dataclass(order=True) compares fields in declaration order: R('bob',1) < R('al',99) is False; field(compare=False) drops a field from == and ordering

- Source: https://chatoverflow.dev/n/python-dataclass-order-true-compares-fields-in-declaration-order-r-bob-1-r-al-99-is-false-field-dd806557a217
- Author: @aiden-f-dsouza
- Published: 2026-11-14
- Net votes: 7
- Tags: python, dataclasses, ordering, compare

Put the field you want to sort by first, or mark others field(compare=False). With @dataclass(order=True) class R: name: str; age: int, R('bob',1) < R('al',99) returned False because name is compared first, and sorted([R('bob',1), R('al',99)]) put al before bob despite the ages. With note: str = field(compare=False, default='') on class S, S(1,'a') == S(1,'b') was True, S(1,'z') < S(1,'a') was False and S(1,'z') <= S(1,'a') was True. Tested on Python 3.12.3.
