locked
How to Sort a list for a specific case c# RRS feed

  • Question

  • User88744855 posted

    here is a sample code where i want get sorted data on TotalCharge property in desending order like this way.

    //List<RateStatus> SortedList =  objListOrder.OrderByDescending(o=>o.TotalCharge).ToList();

    see my below code and guide me how to sort TotalCharge property in desending. thanks

    static class Program
        {
            /// <summary>
            /// The main entry point for the application.
            /// </summary>
            [STAThread]
            static void Main()
            {
                List<RateStatus> oRateCollection = new List<RateStatus>();
                RateStatus oRateStatus = new RateStatus();
    
                oRateStatus.StatusMsg = "Ok";
                oRateStatus.ErrorMsg = "None";
                oRateStatus.RateDetails.Add(new RateDetails()
                {
                    CurrentcyCode = "GBP",
                    TotalCharge = 20
    
                });
    
                oRateCollection.Add(oRateStatus);
    
               // i want something like this below
               //List<RateStatus> SortedList =  objListOrder.OrderByDescending(o=>o.TotalCharge).ToList();
            }
        }
    
        public class RateStatus
        {
            public RateStatus()
            {
                RateDetails = new List<RateDetails>();
            }
    
            public string StatusMsg
            {
                get;
                set;
            }
    
            public string ErrorMsg
            {
                get;
                set;
            }
    
            public List<RateDetails> RateDetails
            {
                get;
                set;
            }
        }
    
        public class RateDetails
        {
            public string ServiceCode
            {
                get;
                set;
            }
    
            public string ServiceName
            {
                get;
                set;
            }
    
            public string CurrentcyCode
            {
                get;
                set;
            }
    
            public double TotalCharge
            {
                get;
                set;
            }
    
        }
    Tuesday, October 28, 2014 5:26 AM

Answers

  • User753101303 posted

    No,  instead you should be always explicit about the current blocking point.

    So as you are ok with Linq, it seems you want to sort oRateCollection rather than, objListOrder. Also as the RateStatus doesn't have a TotalCharge property, my guess is that you want perhaps to sort on the sum of the total charges. It would give for example:

    List<RateStatus> SortedList =  oRateCollection.OrderByDescending(o=>o.RateDetails.Sum(detail=>detail.TotalCharge)).ToList();

    If this is still not what you need, please be explicit about your issue.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, October 28, 2014 7:57 AM

All replies

  • User753101303 posted

    Hi,

    This is part of System.Linq. Add a reference and a using System.Linq as needed...

    Tuesday, October 28, 2014 5:36 AM
  • User88744855 posted

    that was done. just copy my code and test at your end then you can see the problem.

    Tuesday, October 28, 2014 7:30 AM
  • User753101303 posted

    No,  instead you should be always explicit about the current blocking point.

    So as you are ok with Linq, it seems you want to sort oRateCollection rather than, objListOrder. Also as the RateStatus doesn't have a TotalCharge property, my guess is that you want perhaps to sort on the sum of the total charges. It would give for example:

    List<RateStatus> SortedList =  oRateCollection.OrderByDescending(o=>o.RateDetails.Sum(detail=>detail.TotalCharge)).ToList();

    If this is still not what you need, please be explicit about your issue.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, October 28, 2014 7:57 AM
  • User88744855 posted

    why should i use Sum() ??

    Tuesday, October 28, 2014 8:57 AM
  • User753101303 posted

    For now it seems you want to sort RateStatus on the "TotalCharge" but the problem is that this object doesn't have a TotalCharge field.

    Instead they have 0 to n RateDetails so my guess is that you want to sort RateStatus on some value computed from the TotalCharge of its RateDetails (either the sum, max, min, average or whatever that would produce a single value for each RateStatus so that it can be sorted).

    Sum was just just a guess and seemed to make sense to me. Else try to explain in English on which value you want to sort your RateStatus (just TotalCharge won't work as RateStatus don't have this property).

    Tuesday, October 28, 2014 9:41 AM
  • User88744855 posted

    This way it has been sorted.

    oRateStatus.RateDetails.Sort(new RateStatusComparer());
    oRateCollection.Add(oRateStatus);
    
    
    public class RateStatusComparer : IComparer<RateDetails>
        {
            public int Compare(RateDetails x, RateDetails y)
            {
                if (x.TotalCharge > y.TotalCharge) return -1;
                else if (x.TotalCharge < y.TotalCharge) return 1;
                else return 0;
            }
        }

    thanks

    Tuesday, October 28, 2014 10:22 AM
  • User753101303 posted

    Ah great. So you wanted to sort RateDetails for each RateStatus rather than to sort the RateStatus list.

    As you can see, it's best to avoid just showing some code without any explanation. Instead try to always be explicit about the error you have or the thing you want to do so that those trying to help don't have to guess first why the wrong code which is shown doesn't fit your need...

    Tuesday, October 28, 2014 10:39 AM