From f4a1fef3b00c4209a30e6dad055c0492a68125d7 Mon Sep 17 00:00:00 2001 From: Seth Hillbrand Date: Tue, 12 Nov 2019 17:24:57 -0800 Subject: [PATCH] Adjust pin table sort The sort routine requires the value to be true with a is strictly lower than b. But just inverting the '<' doesn't yield strictly lower, it includes '>='. This causes items to jump in the list. --- .../dialogs/dialog_lib_edit_pin_table.cpp | 25 +++++++++++++------ 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/eeschema/dialogs/dialog_lib_edit_pin_table.cpp b/eeschema/dialogs/dialog_lib_edit_pin_table.cpp index 5f5ac4162e..f1d55fc540 100644 --- a/eeschema/dialogs/dialog_lib_edit_pin_table.cpp +++ b/eeschema/dialogs/dialog_lib_edit_pin_table.cpp @@ -246,29 +246,40 @@ public: rhStr = GetValue( rhs, sortCol, units ); } - bool cmp; + bool res; + + // N.B. To meet the iterator sort conditions, we cannot simply invert the truth + // to get the opposite sort. i.e. ~(ab) + auto cmp = [ ascending ]( const auto a, const auto b ) + { + if( ascending ) + return a < b; + else + return b < a; + }; switch( sortCol ) { case COL_NUMBER: case COL_NAME: - cmp = PinNumbers::Compare( lhStr, rhStr ) < 0; + res = cmp( PinNumbers::Compare( lhStr, rhStr ), 0 ); break; case COL_NUMBER_SIZE: case COL_NAME_SIZE: - cmp = ValueFromString( units, lhStr, true ) < ValueFromString( units, rhStr, true ); + res = cmp( ValueFromString( units, lhStr, true ), + ValueFromString( units, rhStr, true ) ); break; case COL_LENGTH: case COL_POSX: case COL_POSY: - cmp = ValueFromString( units, lhStr ) < ValueFromString( units, rhStr ); + res = cmp( ValueFromString( units, lhStr ), ValueFromString( units, rhStr ) ); break; default: - cmp = StrNumCmp( lhStr, rhStr ) < 0; + res = cmp( StrNumCmp( lhStr, rhStr ), 0 ); break; } - return ascending == cmp; + return res; } void RebuildRows( LIB_PINS& aPins, bool groupByName ) @@ -325,7 +336,7 @@ public: void SortRows( int aSortCol, bool ascending ) { std::sort( m_rows.begin(), m_rows.end(), - [ aSortCol, ascending, this ]( LIB_PINS& lhs, LIB_PINS& rhs ) -> bool + [ aSortCol, ascending, this ]( const LIB_PINS& lhs, const LIB_PINS& rhs ) -> bool { return compare( lhs, rhs, aSortCol, ascending, m_userUnits ); } );