我正在尋找排序下列類型數據的更好方法。處理較大的處理大型數據集時,排序算法會導致堆棧溢出?
保存數據看起來像這樣
public class AttributeItem
{
public string AttributeType { get; set; }
public string Title { get; set; }
public string Value { get; set; }
public int ObjectID { get; set; }
public bool CanModify { get; set; }
public bool CanDelete { get; set; }
public bool? IsParent { get; set; }
public int SortID { get; set; }
public int? ParentSortID { get; set; }
public bool Deleted { get; set; }
}
public class AttributeItemNode
{
public AttributeItem Item {get;set;}
public int Depth {get;set;}
public AttributeItemNode(AttributeItem item , int Depth)
{
this.Item = item ;
this.Depth = Depth;
}
}
這裏是到位的結構時,下面的工作正常較小的數據集(在某些系統上它的2000年其他9000),但會導致計算器一個數據的例子需要用一個int來表示它們的深度,並將其分類到一個單獨的對象中。有可能爲孩子的水平去比示例數據
var items = new List<AttributeItem>();
items.Add(new AttributeItem{Title ="Parent1", ObjectID=1,SortID =1, IsParent= true, ParentSortID = Int32.MinValue});
items.Add(new AttributeItem{Title ="FooChild", ObjectID=2,SortID =2, IsParent= false, ParentSortID = 1});
items.Add(new AttributeItem{Title ="Parent2", ObjectID=4,SortID =4, IsParent= true, ParentSortID = Int32.MinValue});
items.Add(new AttributeItem{ Title ="Parent2Child1", ObjectID=5,SortID =5, IsParent= false, ParentSortID = 4});
items.Add(new AttributeItem{Title ="Parent2Child2", ObjectID=7,SortID =7, IsParent= false, ParentSortID = 4});
items.Add(new AttributeItem{Title ="Parent2Child2Child1", ObjectID=6,SortID =6, IsParent= false, ParentSortID = 5});
預期產出將是如下所示的三個層次更深(我從對象中刪除無關的數據,以幫助可讀性)
Depth = 0 Title ="Parent1"
Depth = 1 Title ="FooChild"
Depth = 0 Title ="Parent2"
Depth = 1 Title ="Parent2Child1"
Depth = 2 Title ="Parent2Child2Child1"
Depth = 1 Title ="Parent2Child2"
下面是實際的排序代碼
public static IList<AttributeItemNode> SortAttributeItems(IList<AttributeItem> list)
{
List<AttributeItemNode> newList = new List<AttributeItemNode>();
SortAttributeItems(list, null, 0, newList);
return newList;
}
private static void SortAttributeItems(IList<AttributeItem> list, AttributeItem currentItem, int depth, List<AttributeItemNode> newList)
{
AttributeItem newItem = null;
// look for children
if (currentItem != null)
{
foreach (AttributeItem item in list)
{
if (item.ParentSortID.HasValue && item.ParentSortID.Value != Int32.MinValue && item.ParentSortID.Value == currentItem.SortID)
{
newList.Add(new AttributeItemNode(item, (depth + 1)));
SortAttributeItems(list, item, depth + 1, newList);
}
}
}
if (depth == 0)
{
foreach (AttributeItem item in list)
{
if (!item.ParentSortID.HasValue || item.ParentSortID.Value == Int32.MinValue)
{
if (currentItem == null || item.SortID >= currentItem.SortID)
{
if (newItem == null || newItem.SortID >= item.SortID)
{
newItem = item;
}
}
}
}
}
if (newItem != null)
{
newList.Add(new AttributeItemNode(newItem, depth));
list.Remove(newItem);
SortAttributeItems(list, newItem, depth, newList);
}
}
我看到你的觀點,但在這個問題說明。它會因大量數據而失敗。所以有更多的只是尋找優化。如果它運行速度很慢,數據量較大,那麼我會同意。 –
然後編輯你的標題,詢問*改進*以外的其他內容,這意味着*做出更好的效果*,並且您的第一段提出要求*更好*,這也表示*它可以工作,但我想改進它*。 –
謝謝。我的近身投票已被撤回。 :-) –