You can use custom features to sort multiple attributes of a List and customize a list.

Source: Internet
Author: User

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;
});
}
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.