DropDownList根据下拉项的Text文本序号排序
程序员文章站
2024-03-03 17:41:16
有时候刚好表中没有可以排序的字段,又不想修改表结构,但它的项文本有序号,这时就可以用这方法排序,例如: 测试页default2.aspx: 复制代码 代码如下:
有时候刚好表中没有可以排序的字段,又不想修改表结构,但它的项文本有序号,这时就可以用这方法排序,例如:
测试页default2.aspx:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:dropdownlist runat="server" id="ddltype">
</asp:dropdownlist>
<asp:button runat="server" id="btnsort" onclick="btnsort_click" text="排序" />
</div>
</form>
</body>
</html>
default2.aspx.cs:
using system;
using system.collections.generic;
using system.linq;
using system.web;
using system.web.ui;
using system.web.ui.webcontrols;
using system.text.regularexpressions;
public partial class default2 : system.web.ui.page
{
protected void page_load(object sender, eventargs e)
{
if (!ispostback)
{
ddltype.items.add(new listitem("--请选择--"));
ddltype.items.add(new listitem("2_bb"));
ddltype.items.add(new listitem("1_aa"));
ddltype.items.add(new listitem("4_ee"));
ddltype.items.add(new listitem("3_dd"));
}
}
protected void btnsort_click(object sender, eventargs e)
{
dropdownlistbubblesort(ddltype);
//dropdownlistselectionsort(ddltype);
}
/// <summary>
/// 冒泡排序
/// </summary>
/// <param name="ddl"></param>
public void dropdownlistbubblesort(dropdownlist ddl)
{
listitem listitem = new listitem();
for (int i = 0; i < ddl.items.count; i++)
{
for (int j = i + 1; j < ddl.items.count; j++)
{
int firstval = 0, nextval = 0;
int.tryparse(regex.replace(ddl.items[i].text, @"\d", @"", regexoptions.ignorecase), out firstval);
int.tryparse(regex.replace(ddl.items[j].text, @"\d", @"", regexoptions.ignorecase), out nextval);
if (firstval == 0 || nextval == 0)
continue;
if (firstval > nextval)
{
listitem = ddl.items[j];
ddl.items.remove(ddl.items[j]);
ddl.items.insert(i, listitem);
}
}
}
}
/// <summary>
/// 选择排序
/// </summary>
/// <param name="ddl"></param>
public void dropdownlistselectionsort(dropdownlist ddl)
{
listitem listitem = new listitem();
int length = ddl.items.count;
for (int i = 0; i < length; i++)
{
int min = 0;
int.tryparse(regex.replace(ddl.items[i].text, @"\d", @"", regexoptions.ignorecase), out min);
if (min == 0)
continue;
int minindex = i;
for (int j = i + 1; j < length; j++)
{
int nextval = 0;
int.tryparse(regex.replace(ddl.items[j].text, @"\d", @"", regexoptions.ignorecase), out nextval);
if (nextval == 0)
continue;
if (min > nextval)
{
min = nextval;
minindex = j;
}
}
if (minindex != i)
{
listitem = ddl.items[minindex];
ddl.items.remove(ddl.items[minindex]);
ddl.items.insert(i, listitem);
}
}
}
}
测试页default2.aspx:
复制代码 代码如下:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:dropdownlist runat="server" id="ddltype">
</asp:dropdownlist>
<asp:button runat="server" id="btnsort" onclick="btnsort_click" text="排序" />
</div>
</form>
</body>
</html>
default2.aspx.cs:
复制代码 代码如下:
using system;
using system.collections.generic;
using system.linq;
using system.web;
using system.web.ui;
using system.web.ui.webcontrols;
using system.text.regularexpressions;
public partial class default2 : system.web.ui.page
{
protected void page_load(object sender, eventargs e)
{
if (!ispostback)
{
ddltype.items.add(new listitem("--请选择--"));
ddltype.items.add(new listitem("2_bb"));
ddltype.items.add(new listitem("1_aa"));
ddltype.items.add(new listitem("4_ee"));
ddltype.items.add(new listitem("3_dd"));
}
}
protected void btnsort_click(object sender, eventargs e)
{
dropdownlistbubblesort(ddltype);
//dropdownlistselectionsort(ddltype);
}
/// <summary>
/// 冒泡排序
/// </summary>
/// <param name="ddl"></param>
public void dropdownlistbubblesort(dropdownlist ddl)
{
listitem listitem = new listitem();
for (int i = 0; i < ddl.items.count; i++)
{
for (int j = i + 1; j < ddl.items.count; j++)
{
int firstval = 0, nextval = 0;
int.tryparse(regex.replace(ddl.items[i].text, @"\d", @"", regexoptions.ignorecase), out firstval);
int.tryparse(regex.replace(ddl.items[j].text, @"\d", @"", regexoptions.ignorecase), out nextval);
if (firstval == 0 || nextval == 0)
continue;
if (firstval > nextval)
{
listitem = ddl.items[j];
ddl.items.remove(ddl.items[j]);
ddl.items.insert(i, listitem);
}
}
}
}
/// <summary>
/// 选择排序
/// </summary>
/// <param name="ddl"></param>
public void dropdownlistselectionsort(dropdownlist ddl)
{
listitem listitem = new listitem();
int length = ddl.items.count;
for (int i = 0; i < length; i++)
{
int min = 0;
int.tryparse(regex.replace(ddl.items[i].text, @"\d", @"", regexoptions.ignorecase), out min);
if (min == 0)
continue;
int minindex = i;
for (int j = i + 1; j < length; j++)
{
int nextval = 0;
int.tryparse(regex.replace(ddl.items[j].text, @"\d", @"", regexoptions.ignorecase), out nextval);
if (nextval == 0)
continue;
if (min > nextval)
{
min = nextval;
minindex = j;
}
}
if (minindex != i)
{
listitem = ddl.items[minindex];
ddl.items.remove(ddl.items[minindex]);
ddl.items.insert(i, listitem);
}
}
}
}