返回LINQ大全首页
目录
ThenBy()
- ThenBy()
- ThenByDescending()
使用指定的比较器按升序对序列中的元素执行后续排序。
简单来说就是对使用过OrderBy
和OrderByDescending
的返回序列进行二次排序。(第一次排序优先级最高,如1,2,2,3,二次排序会对2,2排序)
注意不能直接在数组或List上使用。
MSDN
升序:
using System.Linq;
using System.Collections;
using System.Collections.Generic;
public static class Program
{
static void Main( string[] args )
{
string[] fruits = { "grape", "passionfruit", "banana", "mango", "orange", "raspberry", "apple", "blueberry" };
//以排列字符串的长度升序
IOrderedEnumerable orderByFruits = fruits.OrderBy( value => value.Length );
//以字母升序对长度相同的项目进行排序。
IOrderedEnumerable thenByFruits = orderByFruits.ThenBy( value => value );
System.Console.WriteLine("fruits :{0}", fruits.Text());
System.Console.WriteLine("orderBy :{0}", orderByFruits.Text());
System.Console.WriteLine("thenBy :{0}", thenByFruits.Text());
System.Console.ReadKey();
}
public static string Text( this IEnumerable i_source )
{
string text = string.Empty;
foreach( var value in i_source )
{
text += string.Format( "[{0}], ", value );
}
return text;
}
}
fruits :[grape], [passionfruit], [banana], [mango], [orange], [raspberry], [apple], [blueberry],
orderBy :[grape], [mango], [apple], [banana], [orange], [raspberry], [blueberry], [passionfruit],
thenBy :[apple], [grape], [mango], [banana], [orange], [blueberry], [raspberry], [passionfruit],
ThenByDescending()
降序:
using System.Linq;
using System.Collections;
using System.Collections.Generic;
public static class Program
{
static void Main( string[] args )
{
string[] fruits = { "grape", "passionfruit", "banana", "mango", "orange", "raspberry", "apple", "blueberry" };
//以排列字符串的长度升序
IOrderedEnumerable orderByFruits = fruits.OrderBy( value => value.Length );
//以字母降序对长度相同的项目进行排序。
IOrderedEnumerable thenByFruits = orderByFruits.ThenByDescending( value => value );
System.Console.WriteLine("fruits :{0}", fruits.Text());
System.Console.WriteLine("orderBy :{0}", orderByFruits.Text());
System.Console.WriteLine("thenBy :{0}", thenByFruits.Text());
System.Console.ReadKey();
}
public static string Text( this IEnumerable i_source )
{
string text = string.Empty;
foreach( var value in i_source )
{
text += string.Format( "[{0}], ", value );
}
return text;
}
}
fruits :[grape], [passionfruit], [banana], [mango], [orange], [raspberry], [apple], [blueberry],
orderBy :[grape], [mango], [apple], [banana], [orange], [raspberry], [blueberry], [passionfruit],
thenBy :[mango], [grape], [apple], [orange], [banana], [raspberry], [blueberry], [passionfruit],
你也可以指定类中的某元素排序。
using System.Linq;
using System.Collections;
using System.Collections.Generic;
public static class Program
{
private class Parameter
{
public int ID { get; set; }
public string Name { get; set; }
public int Age { get; set; }
public override string ToString()
{
return string.Format( "ID:{0}, Age:{1}, Name:{2},", ID, Age, Name );
}
}
static void Main( string[] args )
{
Parameter[] persons =
{
new Parameter() { ID = 0, Age = 30, Name = "A" },
new Parameter() { ID = 1, Age = 25, Name = "B" },
new Parameter() { ID = 2, Age = 18, Name = "C" },
new Parameter() { ID = 1, Age = 30, Name = "D" },
new Parameter() { ID = 1, Age = 25, Name = "E" },
new Parameter() { ID = 2, Age = 15, Name = "F" },
};
IOrderedEnumerable orderByID = persons.OrderBy( value => value.ID );
IOrderedEnumerable thenByAge = orderByID.ThenBy( value => value.Age );
IOrderedEnumerable thenByName = orderByID.ThenByDescending( value => value.Name );
System.Console.WriteLine("persons :{0}", persons.Text());
System.Console.WriteLine("orderByID :{0}", orderByID.Text());
System.Console.WriteLine("thenByAge :{0}", thenByAge.Text());
System.Console.WriteLine("thenByName :{0}", thenByName.Text());
System.Console.ReadKey();
}
public static string Text( this IEnumerable i_source )
{
string text = string.Empty;
foreach( var value in i_source )
{
text += string.Format( "[{0}], ", value );
}
return text;
}
}
persons :[ID:0, Age:30, Name:A,], [ID:1, Age:25, Name:B,], [ID:2, Age:18, Name:C,], [ID:1, Age:30, Name:D,], [ID:1, Age:25, Name:E,], [ID:2, Age:15, Name:F,],
orderByID :[ID:0, Age:30, Name:A,], [ID:1, Age:25, Name:B,], [ID:1, Age:30, Name:D,], [ID:1, Age:25, Name:E,], [ID:2, Age:18, Name:C,], [ID:2, Age:15, Name:F,],
thenByAge :[ID:0, Age:30, Name:A,], [ID:1, Age:25, Name:B,], [ID:1, Age:25, Name:E,], [ID:1, Age:30, Name:D,], [ID:2, Age:15, Name:F,], [ID:2, Age:18, Name:C,],
thenByName :[ID:0, Age:30, Name:A,], [ID:1, Age:25, Name:E,], [ID:1, Age:30, Name:D,], [ID:1, Age:25, Name:B,], [ID:2, Age:15, Name:F,], [ID:2, Age:18, Name:C,],
但是如果直接使用为实现IComparable
的类进行比较则会报错。
using System.Linq;
using System.Collections;
using System.Collections.Generic;
public static class Program
{
private class Parameter
{
public int ID { get; set; }
public string Name { get; set; }
public int Age { get; set; }
public override string ToString()
{
return string.Format( "ID:{0}, Age:{1}, Name:{2},", ID, Age, Name );
}
}
static void Main( string[] args )
{
Parameter[] persons =
{
new Parameter() { ID = 0, Age = 30, Name = "A" },
new Parameter() { ID = 1, Age = 25, Name = "B" },
new Parameter() { ID = 2, Age = 18, Name = "C" },
new Parameter() { ID = 1, Age = 30, Name = "D" },
new Parameter() { ID = 1, Age = 25, Name = "E" },
new Parameter() { ID = 2, Age = 15, Name = "F" },
};
IOrderedEnumerable orderByID = persons.OrderBy( value => value.ID );
IOrderedEnumerable thenByParam = orderByID.ThenBy( value => value );
try
{
thenByParam.Any();
}
catch( System.Exception i_exception )
{
System.Console.WriteLine( "{0}", i_exception );
System.Console.ReadKey();
return;
}
System.Console.WriteLine("persons :{0}", persons.Text());
System.Console.WriteLine("orderByID :{0}", orderByID.Text());
System.Console.WriteLine("thenByParam :{0}", thenByParam.Text());
System.Console.ReadKey();
}
public static string Text( this IEnumerable i_source )
{
string text = string.Empty;
foreach( var value in i_source )
{
text += string.Format( "[{0}], ", value );
}
return text;
}
}
System.ArgumentException
通过实现IComparable
,可以将其直接排序。
using System.Linq;
using System.Collections;
using System.Collections.Generic;
public static class Program
{
private class Parameter : System.IComparable
{
public int ID { get; set; }
public string Name { get; set; }
public int Age { get; set; }
public int CompareTo( Parameter i_other )
{
if( i_other == null )
{
return -1;
}
int compareID = ID - i_other.ID;
if( compareID != 0 )
{
return compareID;
}
int compareAge = Age - i_other.Age;
if( compareAge != 0 )
{
return compareAge;
}
return string.Compare( Name, i_other.Name );
}
public override string ToString()
{
return string.Format( "ID:{0}, Age:{1}, Name:{2},", ID, Age, Name );
}
}
static void Main( string[] args )
{
Parameter[] persons =
{
new Parameter() { ID = 0, Age = 30, Name = "A" },
new Parameter() { ID = 1, Age = 25, Name = "B" },
new Parameter() { ID = 2, Age = 18, Name = "C" },
new Parameter() { ID = 1, Age = 30, Name = "D" },
new Parameter() { ID = 1, Age = 25, Name = "E" },
new Parameter() { ID = 2, Age = 15, Name = "F" },
};
IOrderedEnumerable orderByID = persons.OrderBy( value => value.ID );
IOrderedEnumerable thenByParam = orderByID.ThenBy( value => value );
System.Console.WriteLine("persons :{0}", persons.Text());
System.Console.WriteLine("orderByID :{0}", orderByID.Text());
System.Console.WriteLine("thenByParam :{0}", thenByParam.Text());
System.Console.ReadKey();
}
public static string Text( this IEnumerable i_source )
{
string text = string.Empty;
foreach( var value in i_source )
{
text += string.Format( "[{0}], ", value );
}
return text;
}
}
persons :[ID:0, Age:30, Name:A,], [ID:1, Age:25, Name:B,], [ID:2, Age:18, Name:C,], [ID:1, Age:30, Name:D,], [ID:1, Age:25, Name:E,], [ID:2, Age:15, Name:F,],
orderByID :[ID:0, Age:30, Name:A,], [ID:1, Age:25, Name:B,], [ID:1, Age:30, Name:D,], [ID:1, Age:25, Name:E,], [ID:2, Age:18, Name:C,], [ID:2, Age:15, Name:F,],
thenByParam :[ID:0, Age:30, Name:A,], [ID:1, Age:25, Name:B,], [ID:1, Age:25, Name:E,], [ID:1, Age:30, Name:D,], [ID:2, Age:15, Name:F,], [ID:2, Age:18, Name:C,],
即使是string、int等也可以通过IComparer
自定义比较规则。
using System.Linq;
using System.Collections;
using System.Collections.Generic;
public static class Program
{
private class CompareString : IComparer
{
public int Compare( string i_lhs, string i_rhs )
{
bool isInitiallyG_L = !string.IsNullOrEmpty( i_lhs ) ? i_lhs.StartsWith( "g" ) : false;
bool isInitiallyG_R = !string.IsNullOrEmpty( i_rhs ) ? i_rhs.StartsWith( "g" ) : false;
if( isInitiallyG_L && !isInitiallyG_R )
{
return -1;
}
if( !isInitiallyG_L && isInitiallyG_R )
{
return 1;
}
return string.Compare( i_lhs, i_rhs );
}
}
static void Main( string[] args )
{
string[] fruits = { "grape", "passionfruit", "banana", "mango", "orange", "raspberry", "apple", "blueberry" };
IOrderedEnumerable orderByFruits = fruits.OrderBy( value => value.Length );
CompareString compare = new CompareString();
IOrderedEnumerable thenByFruits = orderByFruits.ThenBy( value => value, compare);
System.Console.WriteLine("fruits :{0}", fruits.Text());
System.Console.WriteLine("orderBy :{0}", orderByFruits.Text());
System.Console.WriteLine("thenBy :{0}", thenByFruits.Text());
System.Console.ReadKey();
}
public static string Text( this IEnumerable i_source )
{
string text = string.Empty;
foreach( var value in i_source )
{
text += string.Format( "[{0}], ", value );
}
return text;
}
}
fruits :[grape], [passionfruit], [banana], [mango], [orange], [raspberry], [apple], [blueberry],
orderBy :[grape], [mango], [apple], [banana], [orange], [raspberry], [blueberry], [passionfruit],
thenBy :[grape], [apple], [mango], [banana], [orange], [blueberry], [raspberry], [passionfruit],