in SortedObservableCollection/SortedObservableCollection/SortedObservableCollection.cs [69:120]
private int FindNewIndex(T item, bool newItem = true)
{
int index = -1;
if (this.Count == 0)
{ // if the collection is empty, just return 0
return 0;
}
if( this.Count == 1)
{
// if the collection only has 1 item, it's an easy compare
if(compare(item, this[0]) >= 0)
{
return 1;
}
else
{
return 0;
}
}
int firstLargerItem = -1;
// Linear search through collection finding the first larger item
for (int i = 0; i < this.Count; i++)
{
if (compare(item, this[i]) < 0)
{
// break out of loop
firstLargerItem = i;
break;
}
}
if(firstLargerItem == -1)
{
if(newItem)
{
index = this.Count;
}
else
{
index = this.Count - 1;
}
}
else
{
index = firstLargerItem;
}
return index;
}