如何对正对象列表进行排序>否定 - >零 [英] how to sort list of objects in positive-> negative ->zero

查看:78
本文介绍了如何对正对象列表进行排序>否定 - >零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨朋友



我有一个对象列表,其中有一个名为市场价值的字段。我需要根据市场价值按降序对列表进行排序。排序后,我需要从列表中取出前10个值。



但是在拿到前10个时有一些额外的条件。



排序顺序应该是正数,负数,零,

和负数应该是-1,-2,-3 ......



我怎样才能轻松实现这一目标。是否有任何内置功能。<​​br />


  public   static 列表< GetRequestTopPositions> GetRequestTopPositions( string  xmlstring)
{
List< GetRequestTopPositions> list_accr = new 列表< 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)?? < span class =code-string> ;
return sd != null && sd!= 2;
})
。选择(item = > new GetRequestTopPositions
{
IssueType =( string )item.Parent.Parent.Element( IssueType),
符号=( string )item.Parent.Element( 符号),
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 =(字符串)item.Element( 帐户),
KeyAccount =( string )item.Element( KeyAccount ),
数量=( double ?)item.Element( 数量)?? 0D,
MarketValue =( double ?)item.Element( MarketValue)?? 0D,
ChangeInMarketValue =( double ?)item.Element( ChangeInMarketValue)?? 0D
})
.ToList()
.GroupBy(g = > new {g.SecurityDescription,g.Symbol,g.Cusip})
。选择( group = > new GetRequestTopPositions
{
IssueType = group .First()。IssueType,
Symbol = group .First()。符号,
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 );

// 这里我需要应用逻辑
list_accr = result.ToList< GetRequestTopPositions>();
return list_accr;
}

解决方案

我想最简单的方法是在订单中添加一个布尔值(True出现在False之前):< br $>


。OrderByDescending(o = >  o.MarketValue == < span class =code-digit> 0 ) //  或OrderBy(o => o。 MarketValue!= 0) 
.ThenByDescending(o = > o.MarketValue)
.Take( 10 );


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.

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;
        }

解决方案

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

.OrderByDescending(o => o.MarketValue==0) // or OrderBy(o => o.MarketValue!=0)
.ThenByDescending(o => o.MarketValue)
.Take(10);


这篇关于如何对正对象列表进行排序&gt;否定 - >零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆