Object‑c usage 3 (sorting)

Source: Internet
Author: User

 

Object‑c usage 3 (sorting)

SortParameterName records are used to sort data sources at the end.

1. Quasi-renewal

Metric data: One of the ObjectDataSource usage (SelectMethod is used to sort the parameter values)

Add a class Reverser for processing object sorting

Public class Reverser <T>: IComparer <T>

{

Private Type type = null;

Private ReverserInfo info;

Public Reverser (string className, string name, ReverserInfo. Direction direction)

{

Try

{

This. type = Type. GetType (className, true );

This.info. name = name;

This.info. direction = direction;

}

Catch (Exception e)

{

Throw new Exception (e. Message );

}

}

Int IComparer <T>. Compare (T t1, T t2)

{

Object x = this. type. InvokeMember (this.info. name, BindingFlags. Public | BindingFlags. Instance | BindingFlags. GetProperty, null, t1, null );

Object y = this. type. InvokeMember (this.info. name, BindingFlags. Public | BindingFlags. Instance | BindingFlags. GetProperty, null, t2, null );

If (this.info. direction! = ReverserInfo. Direction. ASC)

Swap (ref x, ref y );

Return (new CaseInsensitiveComparer (). Compare (x, y );

}

Private void Swap (ref object x, ref object y)

{

Object temp = null;

Temp = x;

X = y;

Y = temp;

}

}

 

Public struct ReverserInfo

{

Public enum Direction

{

ASC = 0,

DESC,

};

Public enum Target

{

CUSTOMER = 0,

FORM,

FIELD,

SERVER,

};

Public string name;

Public Direction direction;

Public Target target;

}

2. Add the following methods to the business processing class:

Public List <EntityMember> OrderItems (string order)

{

String orderName = order. Split ('') [0];

ReverserInfo. Direction dir = ReverserInfo. Direction. ASC;

If (order. Split (''). Length> 1 & order. Split ('') [1] = "DESC") dir = ReverserInfo. Direction. DESC;

List <EntityMember> result = new List <EntityMember> ();

XmlDocument doc = new XmlDocument ();

Doc. Load (_ path );

XmlNodeList nodes = doc. SelectNodes ("/Members/Member ");

Foreach (XmlNode node in nodes)

{

Result. add (new EntityMember (node. selectSingleNode (". /UID "). innerText, node. selectSingleNode (". /PWD "). innerText, node. selectSingleNode (". /Email "). innerText ));

}

Reverser <EntityMember> reverser = new Reverser <EntityMember> ("EntityMember", orderName, dir );

Result. Sort (reverser );

Return result;

 

Description: when the order is in descending order, the content of the order is: Natural name + space + DESC

When the order is in ascending order, the order content is as follows: Dynamic

3. The content of Aspx indexes is

<Asp: ObjectDataSource ID = "objectperformance1" runat = "server"

SelectMethod = "OrderItems" SortParameterName = "order" TypeName = "Member"> </asp: ObjectDataSource>

<Asp: GridView ID = "GridView1" runat = "server" AutoGenerateColumns = "False"

Performanceid = "objectperformance1" AllowSorting = "true">

<Columns>

<Asp: BoundField DataField = "UID" HeaderText = "UID" SortExprEssion = "UID"/>

<Asp: BoundField DataField = "PWD" HeaderText = "PWD" SortExprEssion = "PWD"/>

<Asp: BoundField DataField = "Email" HeaderText = "Email" SortExprEssion = "Email"/>

</Columns>

</Asp: GridView>

Description: SortParameterName indicates the sequence number used in the method specified by the SelectMethod parameter.

 

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.