[英]Sorting a list that is contained in a sub-class
I'm trying to sort Chromes Bookmarks in alphabetical order with folders first and then urls 我正在尝试按字母顺序排序Chromes书签,首先是文件夹,然后是网址
I have these classes (by Mr. Jason Grimme) 我有这些课程(杰森格里姆先生)
public class Bookmarks
{
public string Checksum { get; set; }
public Root Roots { get; set; }
public int Version { get; set; }
}
/// <summary>
/// Contains 'folders' such as 'Bookmarks bar' and 'Other bookmarks'
/// </summary>
public class Root
{
public RootItem Bookmark_bar { get; set; }
public RootItem Other { get; set; }
}
/// <summary>
/// A folder of bookmarks
/// </summary>
public class RootItem
{
public List<Child> Children { get; set; }
public string Date_added { get; set; }
public string Date_modified { get; set; }
public int Id { get; set; }
public String Name { get; set; }
public string Type { get; set; }
}
/// <summary>
/// Contains information about a specific bookmark
/// </summary>
public class Child
{
public List<Child> Children { get; set; }
public string date_added { get; set; }
public int ID { get; set; }
public string Name { get; set; }
public String Type { get; set; }
public Uri Url { get; set; }
}
I using JavaScriptSerializer to deserialize and serialize the JSON file. 我使用JavaScriptSerializer反序列化和序列化JSON文件。 The file is deserialized OK into the Bookmarks class structure.
该文件被反序列化OK到Bookmarks类结构中。
Now I want to sort the the two lists RootItem.Children and Child.Children so that all folders comes first and then the urls all in alphabetical order. 现在我想对两个列表RootItem.Children和Child.Children进行排序,以便所有文件夹首先出现,然后按字母顺序排列所有文件夹。
How would I do this? 我该怎么做?
Use List.Sort(Comparison<T> comparison)
with a custom Comparison. 将
List.Sort(Comparison<T> comparison)
与自定义比较一起使用。
This is untested code since I don't have your deserialized structure, you might need to spice it up with some null handling if Child.ChildItem
can be null. 这是未经测试的代码,因为我没有您的反序列化结构,如果
Child.ChildItem
可以为null,您可能需要使用一些空处理来增加它。
I also assume that Child.Type tells if the bookmark is a folder or an URL. 我还假设Child.Type告诉书签是文件夹还是URL。 If not, just change the comparisson
return child1.Type.CompareTo(child2.Type);
如果没有,只需更改comparisson
return child1.Type.CompareTo(child2.Type);
to something that sorts by folder/url. 到文件夹/网址排序的东西。
public void SortChildren(List<Child> childList)
{
childList.Sort((child1, child2) =>
{
if (child1.Type == child2.Type)
{
// Sort by name
return child1.Name.CompareTo(child2.Name);
}
else
{
// Sort the type. If this sorts in reverse, swap child1 and child2
return child1.Type.CompareTo(child2.Type);
}
});
// Sort children recursively
foreach(var child in childList)
{
SortChildren(child.Children);
}
}
Start the sorting with 用...开始排序
SortChildren(Root.Other.Children);
Edit 编辑
A more simple example of list sorting is 一个更简单的列表排序示例是
List<int> myInts = new List<int>{1,5,4,3,2};
myInts.Sort(); // default comparer
myInts.Sort((i1, i2) => { return i2.CompareTo(i1); }); // custom "reverse" comparison
If you want them all in the same list, then you have to create an object with the properties you need, like eg MyListItem. 如果您希望它们都在同一个列表中,那么您必须使用所需的属性创建一个对象,例如MyListItem。
I think referencing System.Linq;
我想引用
System.Linq;
and then you can do a getter that select Children.Select(c => new MyListItem( Name = c.Name, etc )
. 然后你可以做一个
select Children.Select(c => new MyListItem( Name = c.Name, etc )
的getter select Children.Select(c => new MyListItem( Name = c.Name, etc )
。
You can then do the same for the urls but append .OrderBy(c => c.Url);
然后,您可以对URL执行相同操作,但附加
.OrderBy(c => c.Url);
Finally you can add the last list to the first, using ToList()
and Append
. 最后,您可以使用
ToList()
和Append
最后一个列表添加到第一个列表。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.