code to Custom sort a list in c#

CzarR 316 Reputation points
2022-03-28T17:54:44.313+00:00

HI, I am trying to custom sort a list. New to c# coding. But here is the code. collist in the very end is throwing an error saying string does not contain a definition for 'codevlaue'. I am trying to sort my 'collist' according to the 'sortOrderList'. Need help with the code. Thanks in advance.

 Stream responseStream = response.GetResponseStream();
                    string responseStr = new StreamReader(responseStream).ReadToEnd();
                    Console.WriteLine(responseStr);
                    //return responseStr;
                    XDocument doc = XDocument.Parse(responseStr);
                    var result = doc.Descendants("QUERY_DATA");
                    List<string> collist = new List<string>();
                    foreach (var item in result)
                    {
                        var atts = item.Attributes();
                        foreach (var att in atts)
                        {
                            collist.Add(att.Name.ToString());
                        }
                    }
                    collist = collist.Distinct().ToList();
                    if (QryName == "AdvanceDetailRpt")
                    {


                        List<string> sortOrderList = new List<string>()
                            {
                            "ClientLoanNum",
                            "CWLLoanNum",
                            "BorlastName",
                            "LineType",
                            "RequestWireAmt",
                            "RequestType",
                            "LoanAmt",
                            "OrigAdvAmt",
                            "AdvanceAmt",
                            "AdvanceDt",
                            "Tranche",
                            "AdjustmentRsn",
                            "OverUnderPosting",
                            "ClosingAgent",
                            "ABANum",
                            "AcctNum",
                            "DraftNum",
                            "FedRefNum",
                            "CashierCheckFedExTrackNum"
                            };
                        collist = collist.OrderBy(i => sortOrderList.IndexOf(i.CodeValue));

                    }
Developer technologies C#
0 comments No comments
{count} votes

Accepted answer
  1. P a u l 10,761 Reputation points
    2022-03-28T19:55:56.047+00:00

    If you replace your last line with this it should work:

    collist = collist.OrderBy(i => sortOrderList.IndexOf(i)).ToList();
    
    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Karen Payne MVP 35,586 Reputation points Volunteer Moderator
    2022-03-28T20:03:47.843+00:00

    CodeValue does not exists as sortOrderList is a List of string thus there are no property for CodeValue. You need to pass in a string value instead.

    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.