110,537
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Globalization;
namespace Geek
{
public class Price
{
public Price(string type, double amount)
{
Type = type;
Amount = amount;
}
public string Type { get; private set; }
public double Amount { get; private set; }
public override string ToString()
{
return string.Format("{0} {1}", Type, Amount);
}
}
class Program
{
static void Main(string[] args)
{
List<Price> prices = new List<Price>()
{
new Price("A", 4),
new Price("A", 5),
new Price("A", 6),
new Price("B", 100),
new Price("B", 120),
new Price("B", 200),
new Price("C", 150),
new Price("C", 300),
new Price("C", 90)
};
List<Price> sortedPrices = prices.Where(p => p.Type == "A").OrderByDescending(p => p.Amount).Union(
prices.Where(p => p.Type != "A").OrderBy(p => p.Amount)).ToList();
Console.WriteLine("Input:");
prices.ForEach(p => Console.WriteLine(p));
Console.WriteLine();
Console.WriteLine("Output:");
sortedPrices.ForEach(p => Console.WriteLine(p));
Console.ReadKey();
}
}
}
无非iCompare(object x,object y){
(y.ptye = b x.pType ==b {
return x.priect - y.price
}
(y.ptye = a x.pType == a {
return (x.priect - y.price) * (-1)
}
else{
类型 不同时的处理
}
}
}
void Main()
{
List<Price> listPrice = new List<Price>
{
new Price { price=4, pType="A" },
new Price { price=5, pType="A" },
new Price { price=6, pType="A" },
new Price { price=100, pType="B" },
new Price { price=120, pType="B" },
new Price { price=200, pType="B" },
new Price { price=150, pType="C" },
new Price { price=300, pType="C" },
new Price { price=90, pType="C" }
};
var list1=listPrice.Where(l=>l.pType=="A").OrderByDescending(l=>l.price);
var list2=listPrice.Except(list1).OrderBy(l=>l.price);
var result=list1.Concat(list2);
}
class Price
{
public int price{get;set;}
public string pType{get;set;}
}