62,268
社区成员
发帖
与我相关
我的任务
分享
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:LinqDataSource ID="LinqDataSource1" runat="server"
onselecting="LinqDataSource1_Selecting" >
</asp:LinqDataSource>
</div>
<asp:GridView ID="GridView1" runat="server"
DataSourceID="LinqDataSource1" AllowSorting="True">
</asp:GridView>
</form>
</body>
</html>
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class _Default : System.Web.UI.Page
{
Stutent[] students;
protected void Page_Load(object sender, EventArgs e)
{
students = new Stutent[]{
new Stutent("shaka",22,"男"),
new Stutent("shaka",12,"男"),
new Stutent("romeo",34,"男"),
new Stutent("rose",45,"女"),
new Stutent("ruby",67,"女"),
new Stutent("kevin",23,"男"),
new Stutent("jordan",22,"男")
};
}
protected void LinqDataSource1_Selecting(object sender, LinqDataSourceSelectEventArgs e)
{
e.Result = students;
}
}
public class Stutent
{
public Stutent(string name, int age, string sex)
{
this.name = name;
this.age = age;
this.sex = sex;
}
private int age;
public int Age
{
get { return age; }
set { age = value; }
}
private string sex;
public string Sex
{
get { return sex; }
set { sex = value; }
}
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace 排序使用委托示例
{
delegate bool Compar(object lhs,object rhs);
class Program
{
static void Main(string[] args)
{
Compar getMethod = new Compar(Employees.compareSalary);//注意不仅可以比工资,还能比名字
Employees[] employees = new Employees[] {
new Employees("shaka",2000),
new Employees("jack",1500),
new Employees("Rose",1200),
new Employees("Ruby",2400),
new Employees("Booal",3000)
};
Sort.sort(employees, getMethod);
for (int i = 0; i < employees.Length; i++)
{
Console.WriteLine(employees[i].ToString());
}
}
}
class Employees
{
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
private decimal salary;
public decimal Salary
{
get { return salary; }
set { salary = value; }
}
public Employees(string name, decimal salary)
{
this.name = name;
this.salary = salary;
}
public static bool compareSalary(object lhs, object rhs)
{
Employees elhs = (Employees)lhs;
Employees erhs = (Employees)rhs;
return (elhs.Salary > erhs.Salary);
}
public static bool compareName(object lhs, object rhs)
{
Employees elhs = (Employees)lhs;
Employees erhs = (Employees)rhs;
return (elhs.Name.Equals(erhs.Name));
}
public override string ToString()
{
return string.Format(name + ",{0:c}", salary);
}
}
class Sort
{
public static void sort(object[] sortArray, Compar getMethod)
{
for (int i = 0; i < sortArray.Length; i++)
{
for (int j = i + 1; j < sortArray.Length; j++)
{
if (getMethod(sortArray[j], sortArray[i]))
{
object temp = sortArray[i];
sortArray[i] = sortArray[j];
sortArray[j] = temp;
}
}
}
}
}
}
using System;
using System.Collections.Generic;
public class DinoComparer: IComparer<string>
{
public int Compare(string x, string y)
{
if (x == null)
{
if (y == null)
{
// If x is null and y is null, they're
// equal.
return 0;
}
else
{
// If x is null and y is not null, y
// is greater.
return -1;
}
}
else
{
// If x is not null...
//
if (y == null)
// ...and y is null, x is greater.
{
return 1;
}
else
{
// ...and y is not null, compare the
// lengths of the two strings.
//
int retval = x.Length.CompareTo(y.Length);
if (retval != 0)
{
// If the strings are not of equal length,
// the longer string is greater.
//
return retval;
}
else
{
// If the strings are of equal length,
// sort them with ordinary string comparison.
//
return x.CompareTo(y);
}
}
}
}
}
public class Example
{
public static void Main()
{
List<string> dinosaurs = new List<string>();
dinosaurs.Add("Pachycephalosaurus");
dinosaurs.Add("Amargasaurus");
dinosaurs.Add("Mamenchisaurus");
dinosaurs.Add("Deinonychus");
Display(dinosaurs);
DinoComparer dc = new DinoComparer();
Console.WriteLine("\nSort with alternate comparer:");
dinosaurs.Sort(dc);
Display(dinosaurs);
SearchAndInsert(dinosaurs, "Coelophysis", dc);
Display(dinosaurs);
SearchAndInsert(dinosaurs, "Oviraptor", dc);
Display(dinosaurs);
SearchAndInsert(dinosaurs, "Tyrannosaur", dc);
Display(dinosaurs);
SearchAndInsert(dinosaurs, null, dc);
Display(dinosaurs);
}
private static void SearchAndInsert(List<string> list,
string insert, DinoComparer dc)
{
Console.WriteLine("\nBinarySearch and Insert \"{0}\":", insert);
int index = list.BinarySearch(insert, dc);
if (index < 0)
{
list.Insert(~index, insert);
}
}
private static void Display(List<string> list)
{
Console.WriteLine();
foreach( string s in list )
{
Console.WriteLine(s);
}
}
}