You can use custom features to sort multiple attributes of a List and customize a list.
I know that linq has the order by function, but I have studied it for a moment. It is a multi-Practice reflection. This is a note and you can directly go to the Code:
Using System;
Using System. Collections. Concurrent;
Using System. Collections. Generic;
Using System. Diagnostics;
Using System. Linq;
Using System. Reflection;
Using System. Security. Cryptography;
Using System. Text;
Using System. Threading. Tasks;
Namespace TestMultiplePropertySort
{
Class Program
{
Static void Main (string [] args)
{
# Region simple test data
Var list = new List <MyClass> ()
{
New MyClass ()
{
P1 = "h3 ",
P2 = 1,
P3 = DateTime. Now
},
New MyClass ()
{
P1 = "h2 ",
P2 = 3,
P3 = DateTime. Now. AddHours (-1)
},
New MyClass ()
{
P1 = "h1 ",
P2 = 2,
P3 = DateTime. Now. AddHours (1)
},
New MyClass ()
{
P1 = "h3 ",
P2 = 1,
P3 = DateTime. Now
},
New MyClass ()
{
P1 = "h1 ",
P2 = 1,
P3 = DateTime. Now
},
New MyClass ()
{
P1 = "h2 ",
P2 = 2,
P3 = DateTime. Now. AddHours (1)
},
};
# Endregion
// Call multi-field sorting
SortMutiplePropertyHelper <MyClass>. SortMutipleProperty (list );
// Reusable
SortMutiplePropertyHelper <MySecondClass>. SortMutipleProperty (new List <MySecondClass> ());
// Output the sorting result
List. ForEach (m => Trace. WriteLine (m. ToString ()));
}
}
Public class MyClass
{
[SortOrder (0)]
Public string P1 {get; set ;}
[SortOrder (1)]
Public int P2 {get; set ;}
[SortOrder (2)]
Public DateTime P3 {get; set ;}
Public override string ToString ()
{
Return P1.ToString () + "," + P2.ToString () + "," + P3.ToString ();
}
}
Public class MySecondClass
{
}
[AttributeUsage (AttributeTargets. Property, AllowMultiple = false, Inherited = false)]
Public class SortOrderAttribute: Attribute
{
Public int Order {get; set ;}
Public SortOrderAttribute (int order)
{
This. Order = order;
}
}
Public class SortMutiplePropertyHelper <T> where T: class, new ()
{
/// <Summary>
/// Dictionary for storing attributes and Sequences
/// </Summary>
Public static readonly Dictionary <PropertyInfo, SortOrderAttribute> attrDic = new Dictionary <PropertyInfo, SortOrderAttribute> ();
Static SortMutiplePropertyHelper ()
{
// Initialize the order Field
Type t = typeof (T );
Foreach (var prop in t. GetProperties ())
{
Foreach (var sortOrderAttribute in prop. GetCustomAttributes (typeof (SortOrderAttribute), false ))
{
If (sortOrderAttribute is SortOrderAttribute)
{
AttrDic. Add (prop, sortOrderAttribute as SortOrderAttribute );
Break;
}
}
}
}
Public static void SortMutipleProperty (List <T> list)
{
List. Sort (t1, t2) =>
{
Int result = 0;
Foreach (var attr in attrDic. OrderBy (key => key. Value. Order ))
{
// Here, we simply convert attributes into strings for comparison. The more reliable method should be to compare different types.
String v1 = attr. Key. GetValue (t1). ToString ();
String v2 = attr. Key. GetValue (t2). ToString ();
Result = v1.CompareTo (v2 );
If (result! = 0)
{
Break;
}
}
Return result;
});
}
}
}