# Lind.DDD.Domain.ISortBehavor～上移与下移

### ISortBehavor内容

class Entity { public int ID{ get; set; } }

interface ISortBehavor { int SortNumber{ get; set; } }

　　/// <summary>
/// 算法帮助类
/// 作者：仓储大叔
/// </summary>
public class AlgorithmsHelper
{
/// <summary>
/// 排序，上移和下移
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="list"></param>
/// <param name="id"></param>
/// <param name="newSort"></param>
public static void Sortable_Up_Down<T>(List<T> list, int id, int newSort) where T : class
{

var old = list.Find(i => (i as Entity).Id == id);
if (old == null || (old as ISortBehavor).SortNumber == newSort)
return;

if ((old as ISortBehavor).SortNumber > newSort)
{
(old as ISortBehavor).SortNumber = newSort;

foreach (ISortBehavor item in list.FindAll(i => (i as Entity).Id != id && (i as ISortBehavor).SortNumber >= newSort))
{

item.SortNumber += 1;
}

}
else
{
(old as ISortBehavor).SortNumber = newSort;

foreach (ISortBehavor item in list.FindAll(i => (i as Entity).Id != id && (i as ISortBehavor).SortNumber <= newSort))
{

item.SortNumber -= 1;
}
}
list.ForEach(i =>
{
Console.WriteLine((i as Entity).Id + "sort:" + (i as ISortBehavor).SortNumber);
}
);
}
}

　 class MainClass
{
public static void Main (string[] args)
{
List<Person> list = new List<Person> ();
}