-3

I have a List as follow:

 List<object> _l = new List<object>();

_l.Add(new {
    Period = _period,
    Ref_No = _ref_no,
    Status = _status,
    Status_Text = _status_text,
    Message = _message
});

The status value would be 0,1 or 2

I don't want to use a Class, How to sort by Status?

Need help

Thanks a lot in advance

Don

Don2
  • 313
  • 3
  • 12
  • 6
    Why do you not want to use a class? – JonasH Jun 03 '20 at 14:25
  • 4
    "I don't want to use a Class" What do you think `List` is? – itsme86 Jun 03 '20 at 14:25
  • I have too many class, I just want to use an object.. is it possible? – Don2 Jun 03 '20 at 14:27
  • Instead of an anonymous class why not use a value tuple (if you're in C#8) `List<(int Period, int Ref_No, int Status, string Status_Text, string Message)>`? Or a `List>` if you're not in C# 8 – juharr Jun 03 '20 at 14:27
  • Or another option is `List` if you want to use an anonymous class, but note that has performance issues and will result in errors that would typically be caught at compile time failing at run time instead. – juharr Jun 03 '20 at 14:31
  • What makes you think you have too many classes? – Broots Waymb Jun 03 '20 at 14:35
  • 1
    This is not a duplicate of https://stackoverflow.com/questions/5461479/sort-list-by-field-c That question is about a list containing a known type. This is a list of anonymous classes cast to `object`. You cannot access the properties without reflection since you don't know the type to cast the `object` too and obviously `object` does not contain the property they want to sort on. – juharr Jun 03 '20 at 14:35
  • How *many* classes is too many? – Ňɏssa Pøngjǣrdenlarp Jun 03 '20 at 14:38
  • anonymous types are meant as temporary data, mostly for use in LINQ queries. Once you convert it to a object there is no good way to access the properties. The primary alternatives are: a) Create a new class. b) Use a Tuple. c) Use a ValueTuple – JonasH Jun 03 '20 at 14:38
  • @ŇɏssaPøngjǣrdenlarp - pretty subjective matter, for one 2 classes are to much, for the other 2000, for the other there is no too many if you need them use them – Rand Random Jun 03 '20 at 14:39
  • @juharr Value tuples were introduced in C# 7, IIRC – Pavel Anikhouski Jun 03 '20 at 14:39
  • 1
    @PavelAnikhouski Yes, my mistake and too late to fix. – juharr Jun 03 '20 at 14:40
  • Change the declaration to `List _l = new List();` and the sort using `_l.Sort((obj1, obj2) => obj1.Status.CompareTo(obj2.Status));` – Jim Hewitt Jun 03 '20 at 15:15

1 Answers1

0

use this function to do it

 public List<Object> SortLST(string SDirection, string propertie, List<Object> data)
    {

        List<Object> data_sorted = new List<Object>();

        if (SDirection == "Ascending")
        {
            data_sorted = (from n in data
                           orderby GetDynamicSortProperty(n, propertie) ascending
                           select n).ToList();
        }
        else if (SDirection == "Descending")
        {
            data_sorted = (from n in data
                           orderby GetDynamicSortProperty(n, propertie) descending
                           select n).ToList();

        }

        return data_sorted;

    }

    public object GetDynamicSortProperty(object item, string propName)
    {
        //Use reflection to get order type
        return item.GetType().GetProperty(propName).GetValue(item, null);
    }
Monarc Dev
  • 26
  • 3