1.直接Insert至指定位置
ObservableCollection<T>方便界面自动更新列表,默认添加至末尾,当列表需要排序时希望能能直接添加至指定位置,下为“经典二分”获取InsertIndex
private int GetIndex(ObservableCollection<Struct> lstData, string name)
{
int index = 0;
if (lstData.Count == 0)
;
int startFlag = 0;
int stopFlag = lstData.Count - 1;
while (startFlag <= stopFlag)
{
if (string.Compare(name, lstData[startFlag].Name) <= 0)
{ index = startFlag; break; }
else if (string.Compare(name, lstData[stopFlag].Name) >= 0)
{ index = stopFlag + 1; break; }
else
{
int temp = (int)(startFlag + stopFlag) / 2;
if (string.Compare(