@@ -147,7 +147,7 @@ private int Heapify(List<OrderByPropertyEntry> dataToSort, OrderByPropertyCompar
147
147
148
148
// Tracking the index is necessary so that unsortable items can be output at the end, in the order
149
149
// in which they were received.
150
- for ( int dataIndex = 0 , discardedDuplicates = 0 ; dataIndex < dataToSort . Count - discardedDuplicates ; dataIndex ++ )
150
+ for ( int dataIndex = 0 , discardedDuplicates = 0 ; dataIndex + discardedDuplicates < dataToSort . Count ; dataIndex ++ )
151
151
{
152
152
// Min-heap: if the heap is full and the root item is larger than the entry, discard the entry
153
153
// Max-heap: if the heap is full and the root item is smaller than the entry, discard the entry
@@ -157,20 +157,19 @@ private int Heapify(List<OrderByPropertyEntry> dataToSort, OrderByPropertyCompar
157
157
}
158
158
159
159
// If we're doing a unique sort and the entry is not unique, discard the duplicate entry
160
- if ( Unique && ! uniqueSet . Add ( dataToSort [ dataIndex ] ) )
160
+ if ( Unique && ! uniqueSet . Add ( dataToSort [ dataIndex + discardedDuplicates ] ) )
161
161
{
162
162
discardedDuplicates ++ ;
163
- if ( dataIndex != dataToSort . Count - discardedDuplicates )
164
- {
165
- // When discarding duplicates, replace them with an item at the end of the list and
166
- // adjust our counter so that we check the item we just swapped in next
167
- dataToSort [ dataIndex ] = dataToSort [ dataToSort . Count - discardedDuplicates ] ;
168
- dataIndex -- ;
169
- }
170
-
163
+ dataIndex -- ;
171
164
continue ;
172
165
}
173
166
167
+ // Shift next non-duplicate entry into place
168
+ if ( discardedDuplicates > 0 )
169
+ {
170
+ dataToSort [ dataIndex ] = dataToSort [ dataIndex + discardedDuplicates ] ;
171
+ }
172
+
174
173
// Add the current item to the heap and bubble it up into the correct position
175
174
int childIndex = dataIndex ;
176
175
while ( childIndex > 0 )
0 commit comments