Skip to content

Commit

Permalink
sorted_robust should sort numeric types as "numbers"
Browse files Browse the repository at this point in the history
  • Loading branch information
jsiirola committed Mar 9, 2021
1 parent 2e4ada2 commit c3946a0
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
6 changes: 5 additions & 1 deletion pyomo/core/base/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import sys
import types

from pyomo.core.expr import native_numeric_types

logger = logging.getLogger('pyomo.core')


Expand Down Expand Up @@ -111,7 +113,9 @@ class _robust_sort_keyfcn(object):
"""
def __init__(self, key=None):
self._typemap = {tuple: (3, tuple.__name__)}
# sort all native numeric types as if they were floats
self._typemap = {t: (1, float.__name__) for t in native_numeric_types}
self._typemap[tuple] =(3, tuple.__name__)
self._key = key

def __call__(self, val):
Expand Down
4 changes: 4 additions & 0 deletions pyomo/core/tests/unit/test_base_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ def test_sorted_robust(self):
a = sorted_robust([3, 2, 1])
self.assertEqual(a, [1, 2, 3])

# Testthat ints and floats are sorted as "numbers"
a = sorted_robust([3, 2.1, 1])
self.assertEqual(a, [1, 2.1, 3])

a = sorted_robust([3, '2', 1])
self.assertEqual(a, [1, 3, '2'])

Expand Down

0 comments on commit c3946a0

Please sign in to comment.