Question

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

Was it helpful?

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.

OTHER TIPS

The SortedList<T> class sorts elements based on a comparison function you provide, or, if you don't provide one, based on the default comparer for the type. Your sorting logic, however, depends on data that's not in the sorted list (you said "I ordered the query by another column). So you can't sort the list.

The List<T> class holds an ordered sequence of elements; the order is the order in which they are added. In other words, List<T> will keep the elements in the same order. Since you want the elements to retain the order in which you retrieved them from the database, You should use List<T>.

no sorry but i have my logic so i have to use sorted list

If you need to use SortedList<T> then you will have to do it in a complicated way so you can refer to the values in the column you used for ordering the data. There are a few possibilities for that. But I would be far more strongly inclined to change the dependency on SortedList<T>. Why do you need to use SortedList<T>? If you can provide more context, we may be able to help you solve the bigger problem. The smaller problem you've reduced it to seems overly complicated.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top