none
Alpha numeric sorting for dataview/datatable RRS feed

  • Question

  • I have values which are numeric and string values. lets say 1,2 ,3 and like xc1,xc2 . I want to sort these values. First sorting numeric ones than none numeric ones. How can I do this with dataview or datatable

    I have tried this one...

    dataview.sort="column1 ASC";

     but its not sorting my data.

    Datatype of column1 is string.

    Is there any way to use IComparable interface to sort dataview data.

    Thanks too much.

    Wednesday, September 17, 2008 2:51 PM

Answers

  • You can use the Linq to DataSet functionality in .Net Framework 3.5.

    You'll need a reference to System.Data.DataSetExtensions.dll

     

    Here is an example where you can provide your own IComparable intefrace to sort the DataView data.

     

    DataTable t1 = new DataTable("PNProduct");

    t1.Columns.Add("c1");

    t1.Rows.Add(new object[] { "1" });

    t1.Rows.Add(new object[] { "a" });

     

    // the p & q queries are only equivalent because how MyComparer was implemented.

    var p = from a in t1.AsEnumerable()

               orderby a.Field<string>("c1")

               select a;

     

    // this needs to be a stable sort that only uses constant data or current data from the row

    IComparer<DataRow> comparer = new MyComparer();

    var q = from a in t1.AsEnumerable()

               orderby comparer

                select a;

     

    DataView view = q.AsDataView();

    DataView b = p.AsDataView();

     

     

    private class MyComparer : IComparer<DataRow> {

      public int Compare(DataRow x, DataRow y) {

        return String.Compare(x.Field<string>("c1", DataRowVersion.Current), y.Field<string>("c1", DataRowVersion.Current));

      }

    }

    Saturday, September 20, 2008 12:01 AM
    Moderator
  • Thanks lot Mark.Ashton.
    I learn lot from your reply.

    But i tried different solution.

    what i did basically, i added one extra column of type int and then load integer value of my actual column to it and using DataView i am getting sorted order.

    this is demo code ..

    DataColumn dc1 = new DataColumn("col1", typeof(string));
    DataColumn dc2 = new DataColumn("col2",typeof(int));
    DataTable dt = new DataTable();
    dt.Columns.Add(dc1);
    dt.Columns.Add(dc2);
    int p = 7;
    for (int i = 0; i < 5; i++)
    {
    dt.Rows.Add();
    dt.RowsIdea[0] = p--;
    }
    int count = dt.Rows.Count;
    dt.Rows.Add();
    dt.Rows[count-1][0] = "zzz";
    dt.Rows.Add();
    dt.Rows[count][0] = "uuu";
    dt.Rows.Add();
    dt.Rows[count + 1][0] = "ccc";

    dt.Rows[count + 2][0] = "aaa";
    for (int i = 0; i < dt.Rows.Count; i++)
    {
    int a;
    if (int.TryParse(dt.RowsIdea[0].ToString(), out a))
    dt.RowsIdea[1] = a;
    }

    DataView dv = dt.DefaultView;
    dv.Sort = "col2 , col1";
    DataTable dtnew = dv.ToTable();


    Thanks,

    Avtar.
    Wednesday, October 8, 2008 12:19 PM

All replies

  • You can use the Linq to DataSet functionality in .Net Framework 3.5.

    You'll need a reference to System.Data.DataSetExtensions.dll

     

    Here is an example where you can provide your own IComparable intefrace to sort the DataView data.

     

    DataTable t1 = new DataTable("PNProduct");

    t1.Columns.Add("c1");

    t1.Rows.Add(new object[] { "1" });

    t1.Rows.Add(new object[] { "a" });

     

    // the p & q queries are only equivalent because how MyComparer was implemented.

    var p = from a in t1.AsEnumerable()

               orderby a.Field<string>("c1")

               select a;

     

    // this needs to be a stable sort that only uses constant data or current data from the row

    IComparer<DataRow> comparer = new MyComparer();

    var q = from a in t1.AsEnumerable()

               orderby comparer

                select a;

     

    DataView view = q.AsDataView();

    DataView b = p.AsDataView();

     

     

    private class MyComparer : IComparer<DataRow> {

      public int Compare(DataRow x, DataRow y) {

        return String.Compare(x.Field<string>("c1", DataRowVersion.Current), y.Field<string>("c1", DataRowVersion.Current));

      }

    }

    Saturday, September 20, 2008 12:01 AM
    Moderator
  • Thanks lot Mark.Ashton.
    I learn lot from your reply.

    But i tried different solution.

    what i did basically, i added one extra column of type int and then load integer value of my actual column to it and using DataView i am getting sorted order.

    this is demo code ..

    DataColumn dc1 = new DataColumn("col1", typeof(string));
    DataColumn dc2 = new DataColumn("col2",typeof(int));
    DataTable dt = new DataTable();
    dt.Columns.Add(dc1);
    dt.Columns.Add(dc2);
    int p = 7;
    for (int i = 0; i < 5; i++)
    {
    dt.Rows.Add();
    dt.RowsIdea[0] = p--;
    }
    int count = dt.Rows.Count;
    dt.Rows.Add();
    dt.Rows[count-1][0] = "zzz";
    dt.Rows.Add();
    dt.Rows[count][0] = "uuu";
    dt.Rows.Add();
    dt.Rows[count + 1][0] = "ccc";

    dt.Rows[count + 2][0] = "aaa";
    for (int i = 0; i < dt.Rows.Count; i++)
    {
    int a;
    if (int.TryParse(dt.RowsIdea[0].ToString(), out a))
    dt.RowsIdea[1] = a;
    }

    DataView dv = dt.DefaultView;
    dv.Sort = "col2 , col1";
    DataTable dtnew = dv.ToTable();


    Thanks,

    Avtar.
    Wednesday, October 8, 2008 12:19 PM