private static IComparer <SaleGoodsData> GetComparerFor(bool desc, bool add, SearchOrderTypes searchOrderType) { IComparer <SaleGoodsData> compare; switch (searchOrderType) { case SearchOrderTypes.OrderByMoney: if (desc && !add) { compare = SaleGoodsMoneyCompare2.Instance; } else { compare = SaleGoodsMoneyCompare.Instance; } return(compare); case SearchOrderTypes.OrderByMoneyPerItem: if (desc && !add) { compare = SaleGoodsMoneyPerItemCompare.DescInstance; } else { compare = SaleGoodsMoneyPerItemCompare.AscInstance; } return(compare); case (SearchOrderTypes)3: break; case SearchOrderTypes.OrderBySuit: if (desc && !add) { compare = SaleGoodsSuitCompare.DescInstance; } else { compare = SaleGoodsSuitCompare.AscInstance; } return(compare); default: if (searchOrderType == SearchOrderTypes.OrderByNameAndColor) { if (desc && !add) { compare = SaleGoodsNameAndColorCompare.DescInstance; } else { compare = SaleGoodsNameAndColorCompare.AscInstance; } return(compare); } break; } compare = SaleGoodsMoneyCompare.Instance; return(compare); }
private static void UpdateOrderdList(List <SaleGoodsData> list, SaleGoodsData saleGoodsData, bool desc, bool add, SearchOrderTypes searchOrderType) { if (add) { if (desc) { list.BinaryInsertDesc(saleGoodsData, SaleManager.GetComparerFor(desc, add, searchOrderType)); } else { list.BinaryInsertAsc(saleGoodsData, SaleManager.GetComparerFor(desc, add, searchOrderType)); } } else { int index = list.BinarySearch(saleGoodsData, SaleManager.GetComparerFor(desc, add, searchOrderType)); if (index < 0) { for (int i = 0; i < list.Count; i++) { if (list[i].GoodsDbID == saleGoodsData.GoodsDbID) { list.RemoveAt(i); break; } } } else { for (int i = index; i >= 0; i--) { if (list[i].GoodsDbID == saleGoodsData.GoodsDbID) { list.RemoveAt(i); return; } } for (int i = index + 1; i < list.Count; i++) { if (list[i].GoodsDbID == saleGoodsData.GoodsDbID) { list.RemoveAt(i); break; } } } } }
/// <summary> /// 按排序类型等条件从列表中添加或删除元素 /// </summary> /// <param name="list"></param> /// <param name="saleGoodsData"></param> /// <param name="desc"></param> /// <param name="add"></param> private static void UpdateOrderdList(List <SaleGoodsData> list, SaleGoodsData saleGoodsData, bool desc, bool add, SearchOrderTypes searchOrderType) { if (add) { if (desc) { list.BinaryInsertDesc(saleGoodsData, GetComparerFor(desc, add, searchOrderType)); } else { list.BinaryInsertAsc(saleGoodsData, GetComparerFor(desc, add, searchOrderType)); } } else { #if false list.Remove(saleGoodsData); #else int index = list.BinarySearch(saleGoodsData, GetComparerFor(desc, add, searchOrderType)); if (index < 0) { //二分查找失败,可能是物品属性变化或不可比较,只能遍历了 for (int i = 0; i < list.Count; i++) { if (list[i].GoodsDbID == saleGoodsData.GoodsDbID) { list.RemoveAt(i); return; } } } else { for (int i = index; i >= 0; i--) { if (list[i].GoodsDbID == saleGoodsData.GoodsDbID) { list.RemoveAt(i); return; } } for (int i = index + 1; i < list.Count; i++) { if (list[i].GoodsDbID == saleGoodsData.GoodsDbID) { list.RemoveAt(i); return; } } } #endif } }