Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to sorting the string field from the List, But not working for me.

Both results are returning the same.

C#
var objNotSort = getLiveData();
var objWithSort = SoryByOperation(getLiveData()).ToArray();


I cannot changes this field to bool Becz. it's web service field.

Can you please help me on this to sort on StartedOrStopped field?

What I have tried:

Class

C#
public class tempLiveData
{
    public string No { get; set; }
    public string StartedOrStopped { get; set; }
    public string Description { get; set; }
}

private static tempLiveData[] SoryByOperation(List<tempLiveData> ObjReadyList)
    {

        GenericSorterLive<tempLiveData> objClassNew = new GenericSorterLive<tempLiveData>();
        var objList = objClassNew.Sort(ObjReadyList, "StartedOrStopped", "No").ToArray();
        return objList;
    }

    private List<tempLiveData> getLiveData()
    {
        objLiveData.Clear();
        objLiveData.Add(new tempLiveData { No = "818012", Description = "Puma", StartedOrStopped = "" });
        objLiveData.Add(new tempLiveData { No = "818012", Description = "Puma - 1", StartedOrStopped = "Yes" });
        objLiveData.Add(new tempLiveData { No = "818012", Description = "Puma - 2", StartedOrStopped = "No" });
        objLiveData.Add(new tempLiveData { No = "818012", Description = "Puma", StartedOrStopped = "No" });
        return objLiveData;
    }

    List<tempLiveData> objLiveData = new List<tempLiveData>();

public class GenericSorterLive<T>
{
    public IEnumerable<T> Sort(IEnumerable<T> source, string sortBy, string sortDirection)
    {
        var param = Expression.Parameter(typeof(T), "item");

        var sortExpression = Expression.Lambda<Func<T, object>>(Expression.Convert(Expression.Property(param, sortBy), typeof(object)), param);

        switch (sortDirection.ToLower())
        {
            case "desc":
                return source.AsQueryable<T>().OrderByDescending<T, object>(sortExpression);
            default:
                return source.AsQueryable<T>().OrderBy<T, object>(sortExpression);

        }
    }
}
Posted
Updated 23-Feb-21 20:28pm
Comments
Maciej Los 24-Feb-21 2:12am    
Have you tried to debug your code?
Bojjaiah 24-Feb-21 2:16am    
Yes, not sorting. That's the reason posted here.

I'm using something like this (via Reflection):

C#
public static class RepoHelper<T>  where T: class
{
    public static List<T> SortBy(List<T> lista, string fieldName, bool ascending = true) 
    {
        Type t = typeof(T);
        PropertyInfo pi = t.GetProperty(fieldName);
        if (ascending)
            lista = lista.OrderBy(x => pi.GetValue(x, null)).ToList();
        else
            lista = lista.OrderByDescending(x => pi.GetValue(x, null)).ToList();

        return lista;
    }
}


Usage:

C#
List<Foo> foos = GetFoo();
//ascending order
foos = RepoHelper<Foo>.SortBy(foos, "Name");
//descending order
foos = RepoHelper<Foo>.SortBy(foos, "ID", false);


You can test it here: RepoHelper | C# Online Compiler | .NET Fiddle[^]
 
Share this answer
 
v2
Comments
Bojjaiah 24-Feb-21 2:28am    
Your code is working descending order. In my case **StartedOrStopped** having value with "No" sort first and then followed "Yes", "".
Maciej Los 24-Feb-21 2:32am    
Which is first in alphabetical order: "Yes" or "No"?
So, if you would like to get proper result:
sortedList = RepoHelper<YourClass>.SortBy(notSortedlist<YourClass>, "StartedOrStopped");
Bojjaiah 24-Feb-21 2:36am    
Great!. In this particular case can we add some condition to get the results in order to "No", "Yes" and "".?
Maciej Los 24-Feb-21 2:42am    
No. If your class holds in [property1] values: {"Yes", "No", "Un"}, then above method sorts list in ascending/descending order by getting the values of property. So, sorting ascending will return: {"No", "Un", "Yes"}.
Bojjaiah 24-Feb-21 2:46am    
Okay thank you for your time. I will up vote this after confirm email address.
The way I'd do it is to add IComparable to the list items class (tempLiveData in this case) and make it a requirement of the GenericSorterLive class type by adding a constraint:
C#
public class GenericSorterLive<T> where T : IComparable
   {
   ...
That way, the class works out how it sorts itself instead of an external class needing to hassle with it.
 
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