A wide-use dynamically multiple sorting library for .NET Core.
You can sort several List by specifying property as string.
- .NET Core 2.0~
- .NET Framework 4.5~
- .NET Standard 2.0~
IOrderedEnumerable<T> OrderBy<T>(this List<T> targetList, string sortKey, SortType sortType = SortType.Asc)
This is a sort method for single sort key.
- List<T> targetList
Sorting target list. - string sortKey
Sort key string.
When you want to sort by deep-hierarchy property, please write this item using "." as delimiter.
e.g.) "SomeClass.SomeInnerClass.SomePropertyName" - SortType sortType
Sort type (SortType.Asc or SortType.Desc).
IOrderedEnumerable<T> OrderBy<T>(this List<T> targetList, Dictionary<string, SortType> sortKeySortTypePairs)
This is a sort method for multiple sort keys.
- List<T> targetList
Sorting target list. - Dictionary<string, SortType> sortKeySortTypePairs
Sort key string and sort type pairs.
When you want to sort by deep-hierarchy property, please write sort key using "." as delimiter.
void MagicSorter.Sort<T>(ref List<T> targetList, string sortKey, SortType sortType = SortType.Asc)
This is a sort method for single sort key.
- List<T> targetList
Sorting target list. - string sortKey
Sort key string.
When you want to sort by deep-hierarchy property, please write this item using "." as delimiter.
e.g.) "SomeClass.SomeInnerClass.SomePropertyName" - SortType sortType
Sort type (SortType.Asc or SortType.Desc).
void MagicSorter.Sort<T>(ref List<T> targetList, Dictionary<string, SortType> sortKeySortTypePairs)
This is a sort method for multiple sort keys.
- List<T> targetList
Sorting target list. - Dictionary<string, SortType> sortKeySortTypePairs
Sort key string and sort type pairs.
When you want to sort by deep-hierarchy property, please write sort key using "." as delimiter.
- Install "MagicSort" NuGet package into your .NET Core project.
- Add using-statement below into your source code.
using MagicSort;
- Call method like below.
var targetList = new List<SomeClass>
{
new SomeClass // This is first item.
{
Prop1 = 123,
Prop2 = 45.6,
Prop3 = "ABC",
Prop4 = new InnerClass
{
InnerProp1 = 23,
InnerProp2 = "45"
InnerProp3 = new DeepClass
{
DeepProp1 = 6.7
}
}
},
// ... some other items here ...
new SomeClass // This is last item.
{
Prop1 = 987,
Prop2 = 65.4,
Prop3 = "ZYX",
Prop4 = new InnerClass
{
InnerProp1 = 54,
InnerProp2 = "32"
InnerProp3 = new DeepClass
{
DeepProp1 = 1.0
}
}
},
};
//========================================
// HOW TO USE METHOD-LIKE STYLE METHODS
//========================================
// Ascending sort by property "Prop1".
MagicSorter.Sort(ref list, "Prop1", SortType.Asc);
// Descending sort by deep property "Prop4.InnerProp3.DeepProp1".
MagicSorter.Sort(ref list, "Prop4.InnerProp3.DeepProp1", SortType.Desc);
// Sorting like
// "ORDER BY Prop2 ASC, Prop4.InnerProp2 DESC".
var sortKeySortTypePairs = new Dictionary<string, SortType>
{
{ "Prop2", SortType.Asc },
{ "Prop4.InnerProp2", SortType.Desc },
};
MagicSorter.Sort(ref list, sortKeySortTypePairs);
//========================================
// HOW TO USE LINQ-LIKE STYLE METHODS
//========================================
var sortedList = new List<SomeClass>();
// Ascending sort by property "Prop1".
sortedList = list
.OrderBy("Prop1", SortType.Asc)
.ToList();
// Descending sort by deep property "Prop4.InnerProp3.DeepProp1".
sortedList = list
.OrderBy("Prop4.InnerProp3.DeepProp1", SortType.Desc)
.ToList();
// Sorting like
// "ORDER BY Prop2 ASC, Prop4.InnerProp2 DESC".
sortedList = list
.OrderBy(new Dictionary<string, SortType>
{
{ "Prop2", SortType.Asc },
{ "Prop4.InnerProp2", SortType.Desc },
})
.ToList();
Added Linq-like methods.
Supported new frameworks and versions below.
- .NET Core 2.0~
- .NET Framework 4.5~
- .NET Standard 2.0~