Search code examples
c#datatablesortedlist

sort sorted list as data table order


i fill sorted list from ordered datatable but after fill sorted list my order changing

     for (int i = 0; i < d.Rows.Count; i++)
        {
              int EmpID = Convert.ToInt32(d.Rows[i]["Fd_Emp"].ToString().Trim());
            string type=d.Rows[i]["Fd_Type"].ToString().Trim();
            emp.Add(EmpID, type);

        }

my datatable order is 31e,32e,33e,7i

my sortedlist order become 7i,31e,32e,33e

how to keep my datatable order in my sorted list


Solution

  • The idea of using a sorted list is that it keeps the added elements in a sorted order. This means that if the elements in the database are not sorted (or not sorted the same way as the sorted list sorts) the elements in the sorted list will have a different order

    If the values in the DB are sorted and you want to get the same ordering from your sorted list, you can provide a custom sorting method for your elements, in which you duplicate the comparison logic that the DB uses for its values.