Click here to Skip to main content
15,895,256 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi friends

I have a list of object in which there is a filed called market value. i need to sort the list in descending order based upon the market value. after sorting i need to take first 10 values from the list.

But there is some extra condition while picking up the first 10.

sorting order should be positive, negative, zero,
and in negative it should be -1,-2,-3...

How can i achieve this in an easy way. is there any built in functionality.

C#
public static List<GetRequestTopPositions> GetRequestTopPositions(string xmlstring)
        {
            List<GetRequestTopPositions> list_accr = new List<GetRequestTopPositions>();          
                XDocument doc = XDocument.Parse(xmlstring);

                var result = doc.Descendants("PositionGroup")
                                .Descendants("PositionSummary")
                                .Descendants("PositionDetail")
                                .Where(item =>
                                {
                                    string sd = (string)item.Parent.Parent.Element("IssueType") ?? "";
                                    return sd != null && sd != "2";
                                })
                                .Select(item => new GetRequestTopPositions
                                     {
                                         IssueType = (string)item.Parent.Parent.Element("IssueType"),
                                         Symbol = (string)item.Parent.Element("Symbol"),
                                         Cusip = (string)item.Parent.Element("Cusip"),
                                         ClosingPrice = (double?)item.Parent.Element("ClosingPrice") ?? 0D,
                                         LatestPrice = (double?)item.Parent.Element("LatestPrice") ?? 0D,
                                         ChangeInPrice = (double?)item.Parent.Element("ChangeInPrice") ?? 0D,
                                         UnitPrice = (double?)item.Parent.Element("UnitPrice") ?? 0D,
                                         PriceFactor = (double?)item.Parent.Element("PriceFactor") ?? 0D,
                                         MSDWSecurityCode = (string)item.Parent.Element("MSDWSecurityCode"),
                                         SecurityDescription = (string)item.Parent.Element("SecurityDescription"),
                                         Office = (string)item.Element("Office"),
                                         Account = (string)item.Element("Account"),
                                         KeyAccount = (string)item.Element("KeyAccount"),
                                         Quantity = (double?)item.Element("Quantity") ?? 0D,
                                         MarketValue = (double?)item.Element("MarketValue") ?? 0D,
                                         ChangeInMarketValue = (double?)item.Element("ChangeInMarketValue") ?? 0D
                                     })
                                  .ToList()
                                  .GroupBy(g => new { g.SecurityDescription, g.Symbol, g.Cusip })
                                  .Select(group => new GetRequestTopPositions
                                     {
                                         IssueType = group.First().IssueType,
                                         Symbol = group.First().Symbol,
                                         Cusip = group.First().Cusip,
                                         ClosingPrice = group.First().ClosingPrice,
                                         LatestPrice = group.First().LatestPrice,
                                         ChangeInPrice=group.First().ChangeInPrice,
                                         UnitPrice = group.First().UnitPrice,
                                         PriceFactor = group.First().PriceFactor,
                                         MSDWSecurityCode = group.First().MSDWSecurityCode,
                                         SecurityDescription = group.First().SecurityDescription,
                                         Office = group.First().Office,
                                         Account = group.First().Account,
                                         KeyAccount = group.First().KeyAccount,
                                         Quantity = group.Sum(q => q.Quantity),
                                         MarketValue = group.Sum(q => q.MarketValue),
                                         ChangeInMarketValue = group.Sum(q => q.ChangeInMarketValue),
                                         Count=group.Count()
                                     })
                                  .OrderByDescending(o => o.MarketValue).Take(10);

//here i need to apply the logic
                           list_accr = result.ToList<GetRequestTopPositions>();
                return list_accr;
        }
Posted
Updated 21-Aug-15 1:47am
v2
Comments
Philippe Mori 21-Aug-15 13:00pm    
Zero is usually between positive and negative numbers. Are you sure you really want to move zeroes at the end? Weird requirement!
jinesh sam 21-Aug-15 15:00pm    
Yes philippe.

1 solution

I guess the easiest way is to add a boolean to the order (True appears before False):

C#
.OrderByDescending(o => o.MarketValue==0) // or OrderBy(o => o.MarketValue!=0)
.ThenByDescending(o => o.MarketValue)
.Take(10);
 
Share this answer
 
v2
Comments
Afzaal Ahmad Zeeshan 21-Aug-15 8:24am    
There's a typo in your post: ThenByDescending not ThenByByDescending. ;-)

I have edited the post and fixed the typo.
Andy Lanng 21-Aug-15 8:34am    
Ha ha - Bye Bye Descending :P

: like i ever test my solutions ^_^
jinesh sam 21-Aug-15 15:00pm    
@Andy Code not working as expected
Andy Lanng 24-Aug-15 3:52am    
how is it working?

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