如何自然排序的东西,如IComparable的数据视图 [英] How to Naturally Sort a DataView with something like IComparable

查看:110
本文介绍了如何自然排序的东西,如IComparable的数据视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据视图的活动有异常,它是排序按字母顺序的事情,我需要它的东西数字排序。我期待所有在网络这一块,发现如何与ICompare排序很多想法,但没有真正稳固。

所以,我的问题是

  1. 如何实现ICompare上一个DataView(寻找code在这里)。
  2. 如何正确地从全是实际的字符串和全数字列(用逗号)字符串的列破译。

我需要code来帮助我走出这个家伙。我或多或少ICompare的想法,以及如何在不同的方案来实现这样一个对所有很好的解释将是巨大的丧失。

另外,请不要递给我联系。我在寻找在这一个坚实的答案。

这是我用一些code。

 数据视图数据视图=(数据视图)会议[kingdomData];
    dataView.Sort = e.SortEx pression ++ ConvertSortDirectionToSql(e.SortDirection);
    gvAllData.DataSource =数据视图;
    gvAllData.DataBind();



    私人字符串ConvertSortDirectionToSql(SortDirection sortDirection)
{
    字符串newSortDirection =的String.Empty;
    如果(会话[SortDirection] == NULL)
    {
        开关(sortDirection)
        {
            案例SortDirection.Ascending:
                newSortDirection =ASC;
                打破;
            案例SortDirection.Descending:
                newSortDirection =DESC;
                打破;
        }
    }
    其他
    {
        newSortDirection =会话[SortDirection]的ToString()。
        开关(newSortDirection)
        {
            案ASC:
                newSortDirection =DESC;
                打破;
            案DESC:
                newSortDirection =ASC;
                打破;
        }
    }
    会话[SortDirection] = newSortDirection;
    返回newSortDirection;
}
 

解决方案

对于第一个问题 - 这个都不能进行排序与​​比较器数据视图。如果你只需要数字排序字段,你必须确保该列类型是数字,而不是字符串。有些code将有助于阐明这一点。

对于第二个问题,你也可以这样做,直接在数据视图。如果你真的需要记录的基础上的数据在一列一些处理排序,那么我将数据复制在一个阵列和阵列上使用的IComparer:

 数据视图DV =新的数据视图(DT);
ArrayList的LST =新的ArrayList();
lst.AddRange(dv.Table.Rows);
lst.Sort(新MyComparer());
的foreach(DataRow的博士在LST)
的Debug.WriteLine(博士[0]);
 

该比较器是这样的:

 类MyComparer:的IComparer
{
公众诠释比较(对象x,对象Y)
{
DataRow的RX = x作为DataRow的;
DataRow的RY = Y为DataRow的;
字符串数据X =(字符串)RX [COLNAME]
字符串DATAY =(字符串)RY [COLNAME]
//处理数据X和DATAY这里,那么对它们进行比较(ASC)
返回datax.CompareTo(DATAY);
}
}
 

这会增加内存消耗,所以你需要考虑是否有可能是更好的方式来preprocess表中的数据,这样就可以直接排序的数据视图由一列。

P.S。 COLNAME是你有兴趣排序的列名。替换为实际的code中的注释提取列的排序信息。你也可以用这种方法来提取更多的列排序信息。只需使用是这样的:

  INT CMP = colAx.CompareTo(colAy);
如果(CMP!= 0)
返回CMP;
CMP = colBy.CompareTo(colBx);
返回CMP;
 

这将可乐的值进行比较升序,然后通过COLB值递减(不是第二次比较具有的的先升后的 X 的)

修改:好吧,我跨preTED错误的逗号分隔值的术语。从你的榜样,我认为你实际上意味着数以千位分隔符(1,000,000 =一百万)。如果存储的数字像这样在数据库中,那么它必须是你正在使用的文本字段,而应该是你的排列顺序是字母数字的原因。

根据这一假设我建议改变该列数字的类型,维持正常数量的内部和格式化(与千位分隔符),只有当你显示它们。这样的排序应在数据视图直接工作,你不必复制数据。

My DataView is acting funny and it is sorting things alphabetically and I need it to sort things numerically. I have looked all across the web for this one and found many ideas on how to sort it with ICompare, but nothing really solid.

So my questions are

  1. How do I implement ICompare on a DataView (Looking for code here).
  2. How to correctly decipher from a column full of strings that are actual strings and a column full of numbers(with commas).

I need code to help me out with this one guys. I am more or less lost on the idea of ICompare and how to implement in different scenarios so an over all good explanation would be great.

Also, Please don't hand me links. I am looking for solid answers on this one.

Some Code that I use.

    DataView dataView = (DataView)Session["kingdomData"];
    dataView.Sort = e.SortExpression + " " + ConvertSortDirectionToSql(e.SortDirection);
    gvAllData.DataSource = dataView;
    gvAllData.DataBind();



    private string ConvertSortDirectionToSql(SortDirection sortDirection)
{
    string newSortDirection = String.Empty;
    if (Session["SortDirection"] == null)
    {
        switch (sortDirection)
        {
            case SortDirection.Ascending:
                newSortDirection = "ASC";
                break;
            case SortDirection.Descending:
                newSortDirection = "DESC";
                break;
        }
    }
    else
    {
        newSortDirection = Session["SortDirection"].ToString();
        switch (newSortDirection)
        {
            case "ASC":
                newSortDirection = "DESC";
                break;
            case "DESC":
                newSortDirection = "ASC";
                break;
        }
    }
    Session["SortDirection"] = newSortDirection;
    return newSortDirection;
}

解决方案

For the first issue - IIRC you can't sort a DataView with a comparer. If you just need to sort numerically a field you must be sure that the column type is numeric and not string. Some code would help to elucidate this.

For the second issue also you can't do that directly in the DataView. If you really need to sort the records based on some processing of data in a column then I'd copy the data in an array and use an IComparer on the array:

DataView dv = new DataView(dt);
ArrayList lst = new ArrayList();
lst.AddRange(dv.Table.Rows);
lst.Sort(new MyComparer());
foreach (DataRow dr in lst)
	Debug.WriteLine(dr[0]);

The comparer is like this:

	class MyComparer : IComparer
	{
		public int Compare(object x, object y)
		{
			DataRow rx = x as DataRow;
			DataRow ry = y as DataRow;
			string datax = (string)rx[colName];
			string datay = (string)ry[colName];
			// Process datax and datay here then compare them (ASC)
			return datax.CompareTo(datay);
		}
	}

This will increase the memory consumption, so you need to think if there is maybe a better way to preprocess the data in the table so that you can sort directly the DataView by a column.

P.S. colName is the name of the column you're interested to sort by. Replace the comment with actual code to extract the sorting info from the column. You can also use this method to extract sorting info from more columns. Just use something like this:

int cmp = colAx.CompareTo(colAy);
if (cmp != 0)
	return cmp;
cmp = colBy.CompareTo(colBx);
return cmp;

This will compare ascending by colA values and then descending by colB values (not that the second compare has y first and then x)

Edit: OK, I interpreted wrongly the term comma separated values. From your example I think that you actually meant numbers with thousand separators (1,000,000 = one million). If you store numbers like this in the database then it must be that you're using a text field and that should be the reason your sorting order is alphanumeric.

Based on this assumption I would propose to change the type of that column to numeric, keep normal numbers inside and format them (with thousand separators) only when you display them. This way the sort should work directly in the DataView and you don't have to copy the data.

这篇关于如何自然排序的东西,如IComparable的数据视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆