List<price> listPrice = new List<price>()
privce pType
4 A
5 A
6 A
100 B
120 B
200 B
150 C
300 C
90 C要求type为A的为price的降序,type B和C的price为升序
privce pType
6 A
5 A
4 A
90 C
100 B
120 B
150 C
200 B
300 C
privce pType
4 A
5 A
6 A
100 B
120 B
200 B
150 C
300 C
90 C要求type为A的为price的降序,type B和C的price为升序
privce pType
6 A
5 A
4 A
90 C
100 B
120 B
150 C
200 B
300 C
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;}
}
在对比的接口中实现。
无非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{
类型 不同时的处理
}
}
}
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();
}
}
}
I roughly wrote a sample. Hope this helps.