- оценок учащихся
Код: Выделить всё
existingList
- оценок учащихся
Код: Выделить всё
newList
[*]найдите разницу между новым списком и старым списком,
[*]затем найдите наименее навязчивый способ вставки или обновления в новый список любых изменений.
< /ul>
Какой алгоритм лучше всего подходит для этого? Хотите сосредоточиться на минимальном количестве изменений в новом списке и производительности.
Пример кода:
Код: Выделить всё
List existingList = new List();
List newList = new List();
public TopLists()
{
InitTwoLists();
}
private void InitTwoLists()
{
existingList.Add(new ListItem { Name = "Shane", Score = 100 });
existingList.Add(new ListItem { Name = "Mark", Score = 95 });
existingList.Add(new ListItem { Name = "Shane", Score = 94 });
existingList.Add(new ListItem { Name = "Steve", Score = 90 });
existingList.Add(new ListItem { Name = "Brian", Score = 85 });
existingList.Add(new ListItem { Name = "Craig", Score = 85 });
existingList.Add(new ListItem { Name = "John", Score = 82 });
existingList.Add(new ListItem { Name = "Steve", Score = 81 });
existingList.Add(new ListItem { Name = "Philip", Score = 79 });
existingList.Add(new ListItem { Name = "Peter", Score = 70 });
newList.Add(new ListItem { Name = "Shane", Score = 100 });
// This is change
newList.Add(new ListItem { Name = "Steve", Score = 96 });
newList.Add(new ListItem { Name = "Mark", Score = 95 });
newList.Add(new ListItem { Name = "Shane", Score = 94 });
newList.Add(new ListItem { Name = "Brian", Score = 85 });
newList.Add(new ListItem { Name = "Craig", Score = 85 });
newList.Add(new ListItem { Name = "John", Score = 82 });
newList.Add(new ListItem { Name = "Steve", Score = 81 });
newList.Add(new ListItem { Name = "Philip", Score = 79 });
newList.Add(new ListItem { Name = "Peter", Score = 70 });
}
}
Код: Выделить всё
public void CompareLists()
{
// How would I find the deltas and update the
// new list with any changes from old?
}
}
public class ListItem
{
public string Name { get; set; }
public int Score { get; set; }
}
Желаемый результат — фактически изменить newList с помощью дельт.
Например. в этом сценарии:
Код: Выделить всё
newList.Add(new ListItem { Name = "Shane", Score = 100 });
newList.Add(new ListItem { Name = "Steve", Score = 96 }); // This is change
newList.Add(new ListItem { Name = "Mark", Score = 95 });
newList.Add(new ListItem { Name = "Shane", Score = 94 });
newList.Add(new ListItem { Name = "Brian", Score = 85 });
newList.Add(new ListItem { Name = "Craig", Score = 85 });
newList.Add(new ListItem { Name = "John", Score = 82 });
newList.Add(new ListItem { Name = "Steve", Score = 81 });
newList.Add(new ListItem { Name = "Roger", Score = 80 }); // Roger is a new entry
newList.Add(new ListItem { Name = "Phillip", Score = 79 }); // Philip moved down one
Итак, изменения будут такими:< /p>
Обновить запись 2 для «Стив», счет изменился.
Вставить новую запись «Роджер» на позицию 9.
Удалить запись «Питер» из топ-10. .
Подробнее здесь: https://stackoverflow.com/questions/368 ... find-the-d