/// <summary> /// Insert a book into the appropriate place. If there is already a book with the same FolderPath, replace it. /// </summary> /// <param name="bookInfo"></param> public void InsertBookInfo(BookInfo bookInfo) { IComparer <string> comparer = new NaturalSortComparer <string>(); for (int i = 0; i < _bookInfos.Count; i++) { var compare = comparer.Compare(_bookInfos[i].FolderPath, bookInfo.FolderPath); if (compare == 0) { _bookInfos[i] = bookInfo; // Replace return; } if (compare > 0) { _bookInfos.Insert(i, bookInfo); return; } } _bookInfos.Add(bookInfo); }
public void UpdateBookInfo(BookInfo info) { var oldIndex = _bookInfos.FindIndex(i => i.Id == info.Id); IComparer <string> comp = new NaturalSortComparer <string>(); var newKey = Path.GetFileName(info.FolderPath); if (oldIndex >= 0) { // optimize: very often the new one will belong at the same index, // if that's the case we could just replace. _bookInfos.RemoveAt(oldIndex); } int newIndex = _bookInfos.FindIndex(x => comp.Compare(newKey, Path.GetFileName(x.FolderPath)) <= 0); if (newIndex < 0) { newIndex = _bookInfos.Count; } _bookInfos.Insert(newIndex, info); NotifyCollectionChanged(); }