I'm trying to sort Chromes Bookmarks in alphabetical order with folders first and then urls
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. The file is deserialized OK into the Bookmarks class structure.
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.
How would I do this?
Use List.Sort(Comparison<T> comparison)
with a custom 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.
I also assume that Child.Type tells if the bookmark is a folder or an URL. If not, just change the 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.
I think referencing System.Linq;
and then you can do a getter that 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);
Finally you can add the last list to the first, using ToList()
and Append
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.