【发布时间】:2016-10-01 02:03:17
【问题描述】:
我正在寻找一种更好的方法来对以下类型的数据进行排序。以下适用于较小的数据集(在某些系统上是 2000 和其他 9000),但在处理较大的数据集时会导致堆栈溢出
保存数据的结构如下所示
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;
}
}
这是一个需要将数据分类为单个对象的示例,其中一个 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);
}
}
【问题讨论】:
-
我明白你的意思,但正如问题中所述。它因大量数据而失败。因此,除了寻找优化之外,还有更多。如果它只是在使用更大的数据集时运行速度真的很慢,那么我会同意。
-
然后编辑你的标题以要求改进以外的其他内容,这意味着做一些更好的工作,以及你的第一段,询问关于让它更好,这也说它有效,但我想改进它。
-
谢谢。我的近距离投票已被撤回。 :-)
-
正如我所见,
depth也是根据可用的层次结构/数据进行计算的,默认情况下不提供 -
请说明(接近文章开头)为什么您使用自己的排序程序而不是使用现有的排序程序。