Click here to Skip to main content
15,949,686 members
Please Sign up or sign in to vote.
1.50/5 (2 votes)
See more:
I have this code to sort/order a List but it does not work

C#
public List<List<string>> OrderBy()
    {
        List<List<string>> listOrderBy = new List<List<string>>();
        for (int i = 0; i <= gvFilters.Rows.Count - 1; i++)
        {
            List<string> track = new List<string>();
            TextBox txtOrder = (TextBox)gvFilters.Rows[i].FindControl("txtOrder");
            DropDownList drpTableName = (DropDownList)gvFilters.Rows[i].FindControl("drpTableName");
            DropDownList drpFieldName = (DropDownList)gvFilters.Rows[i].FindControl("drpFieldName");
            track.Add(txtOrder.Text);
            track.Add(drpTableName.SelectedValue.ToString() + "." + drpFieldName.SelectedValue.ToString());
            listOrderBy.Add(track);
        }
        listOrderBy.OrderBy(l => Double.Parse(l[0]));
        return listOrderBy;
    }


My input are these:

1, Salutation
4, LastName
3, MiddleName
2, FirstName
5, Gender

The output should be

1, Salutation
2, FirstName
3, MiddleName
4, LastName
5, Gender

What I have tried:

I tried the code above and it did not work.
Posted
Comments
Sergey Alexandrovich Kryukov 15-Feb-16 22:30pm    
There is no such thing as "multi-dimensional list".
—SA

1 solution

To share to others engaging to this kind of problem. Here's the solution I found:

Need to create a class for your requirement

C#
public class OrderFields
{
    public int OrderNo { get; set; }
    public string OrderField { get; set; }

    public OrderFields(int num, string field)
    {
        OrderNo = num;
        OrderField = field;
    }
}


Then use this class as your List<>

C#
public List<OrderFields> OrderBy()
    {
        List<OrderFields> listOrderBy = new List<OrderFields>();
        for (int i = 0; i <= gvFilters.Rows.Count - 1; i++)
        {
            TextBox txtOrder = (TextBox)gvFilters.Rows[i].FindControl("txtOrder");
            DropDownList drpTableName = (DropDownList)gvFilters.Rows[i].FindControl("drpTableName");
            DropDownList drpFieldName = (DropDownList)gvFilters.Rows[i].FindControl("drpFieldName");
            string ordervalue = drpTableName.SelectedValue.ToString() + "." + drpFieldName.SelectedValue.ToString();
            listOrderBy.Add(new OrderFields(Convert.ToInt32(txtOrder.Text), ordervalue));
        }
        listOrderBy.Sort(delegate(OrderFields x, OrderFields y)
        {
            return x.OrderNo.CompareTo(y.OrderNo);
        });
        return listOrderBy;
    }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900