diff --git a/pyomo/core/base/block.py b/pyomo/core/base/block.py index 3c642427086..5063073bd12 100644 --- a/pyomo/core/base/block.py +++ b/pyomo/core/base/block.py @@ -1359,7 +1359,7 @@ def _component_data_iter(self, ctype=None, active=None, sort=False): _items = ((None, comp),) if _sort_indices: - _items = sorted_robust(_items) + _items = sorted_robust(_items, key=itemgetter(0)) if active is None or not isinstance(comp, ActiveIndexedComponent): for idx, compData in _items: yield (name, idx), compData diff --git a/pyomo/core/tests/unit/test_block.py b/pyomo/core/tests/unit/test_block.py index ac6710c9186..0fce0f14117 100644 --- a/pyomo/core/tests/unit/test_block.py +++ b/pyomo/core/tests/unit/test_block.py @@ -342,6 +342,15 @@ def test_Block(self): self.assertEqual(sorted([id(comp) for comp in model.block_data_objects(sort=False)]), sorted([id(comp) for comp in [model,]+model.component_data_lists[Block]])) + def test_mixed_index_type(self): + m = ConcreteModel() + m.I = Set(initialize=[1,'1',3.5,4]) + m.x = Var(m.I) + v = list(m.component_data_objects(Var, sort=True)) + self.assertEqual(len(v), 4) + for a,b in zip([m.x[1], m.x[3.5], m.x[4], m.x['1']], v): + self.assertIs(a, b) + class HierarchicalModel(object): def __init__(self):