程序代码
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="XXXX.BLL" %>
<%@ Import Namespace="XXXX.Model" %>
<%@ Import Namespace="XXXX.DBUtility" %>
<script runat="server">
protected string stext;
protected void Page_Load(object sender, EventArgs e)
{
StringBuilder sb = new StringBuilder();
using (System.Data.SqlClient.SqlDataReader rd = XXXX.DBUtility.SqlHelper.ExecuteReader(SqlHelper.ConnectionString, System.Data.CommandType.Text, "Select Kinds + 'http://www.cuoxin.com/' FROM XXX", null))
{
while (rd.Read())
{
sb.Append(rd.GetString(0));
}
}
stext = sb.ToString();
ToArrayBySort(ToArray(stext, 'http://www.cuoxin.com/'));
}
/// <summary>
/// 将字符串根据分隔符转化为数组
/// </summary>
/// <param name="sourcestring">要转化的字符串</param>
/// <param name="compart">分隔符</param>
/// <returns></returns>
public ArrayList ToArray(string sourcestring, char split)
{
CharEnumerator ce = sourcestring.GetEnumerator();
StringBuilder sb = new StringBuilder();
ArrayList slist = new ArrayList();
while (ce.MoveNext())
{
if (ce.Current != split)
{
sb.Append(ce.Current);
}
else
{
if (string.Empty == sb.ToString()) continue;
slist.Add(sb.ToString());
sb.Remove(0, sb.ToString().Length);
}
}
return slist;
}
public class myComparer : IComparer
{
int IComparer.Compare(Object x, Object y)
{
return ((new CaseInsensitiveComparer()).Compare(((SortItem)y).Count, ((SortItem)x).Count));
}
}
public class SortItem
{
private string itemname;
private int count;
public SortItem()
{
}
public string ItemName
{
get { return itemname; }
set { itemname = value; }
}
public int Count
{
get { return count; }
set { count = value; }
}
}
public System.Collections.Generic.IList<SortItem> ToArrayBySort(ArrayList slist)
{
slist.Sort();
ArrayList sortList = new ArrayList();
foreach (object obj in slist)
{
SortItem sItem = new SortItem();
sItem.ItemName = obj.ToString();
sItem.Count = 1;
if (sortList.Count == 0) { sortList.Add(sItem); continue; }
if (obj.ToString() == ((SortItem)sortList[sortList.Count - 1]).ItemName)
{
sItem.Count = ((SortItem)sortList[sortList.Count - 1]).Count + 1;
sortList.RemoveAt(sortList.Count - 1);
}
sortList.Add(sItem);
}
myComparer myCm = new myComparer();
sortList.Sort(myCm);
System.Collections.Generic.IList<SortItem> iList = new System.Collections.Generic.List<SortItem>();
foreach (object obj in sortList)
{
iList.Add((SortItem)obj);
//Response.Write(((SortItem)obj).ItemName + "-" + ((SortItem)obj).Count.ToString() + "<br />");
}
return iList;
}
</script>