LINQ之OrderBy,OrderByDescending

返回LINQ大全首页

目录

  • OrderBy()
  • OrderByDescending()

OrderBy()

按升序对序列的元素进行排序。
MSDN

以下示例使用lambda表达式返回元素本身。

using System.Linq;
using System.Collections;
using System.Collections.Generic;

public static class Program
{
    static void Main( string[] args )
    {
        int[]       dataA   = new int[] { 3, 1, 2, 0, 4 };
        List<float> dataB   = new List<float>() { 1.5f, 1.3f, 3.2f };
        string[]    dataC   = new string[] { "正一郎", "清次郎", "誠三郎", "征史郎" };

        IOrderedEnumerable<int>     orderedDataA    = dataA.OrderBy( value => value );
        IOrderedEnumerable<float>   orderedDataB    = dataB.OrderBy( value => value );
        IOrderedEnumerable<string>  orderedDataC    = dataC.OrderBy( value => value );

        System.Console.WriteLine( "dataA        :{0}", dataA.Text() );
        System.Console.WriteLine( "dataA ordered:{0}", orderedDataA.Text() );
        System.Console.WriteLine( "dataB        :{0}", dataB.Text() );
        System.Console.WriteLine( "dataB ordered:{0}", orderedDataB.Text() );
        System.Console.WriteLine( "dataC        :{0}", dataC.Text() );
        System.Console.WriteLine( "dataC ordered:{0}", orderedDataC.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;
    }
}

dataA :[3], [1], [2], [0], [4],
dataA ordered:[0], [1], [2], [3], [4],
dataB :[1.5], [1.3], [3.2],
dataB ordered:[1.3], [1.5], [3.2],
dataC :[正一郎], [清次郎], [誠三郎], [征史郎],
dataC ordered:[征史郎], [正一郎], [清次郎], [誠三郎],

可以指定根据特定元素进行排列。

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 override string ToString()
        {
            return string.Format( "ID:{0}, Name:{1}", ID, Name );
        }
    }

    static void Main( string[] args )
    {
        Parameter[] parameters = new Parameter[]
        {
            new Parameter() { ID = 0, Name = "正一郎" },
            new Parameter() { ID = 3, Name = "清次郎" },
            new Parameter() { ID = 2, Name = "誠三郎" },
            new Parameter() { ID = 5, Name = "征史郎" },
        };

        IOrderedEnumerable<Parameter>   orderedParameters   = parameters.OrderBy( value => value.ID );

        System.Console.WriteLine( "parameters        :{0}", parameters.Text() );
        System.Console.WriteLine( "parameters ordered:{0}", orderedParameters.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;
    }

} 

parameters :[ID:0, Name:正一郎], [ID:3, Name:清次郎], [ID:2, Name:誠三郎], [ID:5, Name:征史郎],
parameters ordered:[ID:0, Name:正一郎], [ID:2, Name:誠三郎], [ID:3, Name:清次郎], [ID:5, Name:征史郎],

OrderByDescending()

您可以使用OrderByDescending()降序排序。
用法与OrderBy()相同。

using System.Linq;
using System.Collections;
using System.Collections.Generic;

public static class Program
{
    static void Main( string[] args )
    {
        int[]       dataA   = new int[] { 3, 1, 2, 0, 4 };
        List<float> dataB   = new List<float>() { 1.5f, 1.3f, 3.2f };
        string[]    dataC   = new string[] { "正一郎", "清次郎", "誠三郎", "征史郎" };

        IOrderedEnumerable<int>     orderedDataA    = dataA.OrderByDescending( value => value );
        IOrderedEnumerable<float>   orderedDataB    = dataB.OrderByDescending( value => value );
        IOrderedEnumerable<string>  orderedDataC    = dataC.OrderByDescending( value => value );

        System.Console.WriteLine( "dataA        :{0}", dataA.Text() );
        System.Console.WriteLine( "dataA ordered:{0}", orderedDataA.Text() );
        System.Console.WriteLine( "dataB        :{0}", dataB.Text() );
        System.Console.WriteLine( "dataB ordered:{0}", orderedDataB.Text() );
        System.Console.WriteLine( "dataC        :{0}", dataC.Text() );
        System.Console.WriteLine( "dataC ordered:{0}", orderedDataC.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;
    }
} 
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 override string ToString()
        {
            return string.Format( "ID:{0}, Name:{1}", ID, Name );
        }
    }

    static void Main( string[] args )
    {
        Parameter[] parameters = new Parameter[]
        {
            new Parameter() { ID = 0, Name = "正一郎" },
            new Parameter() { ID = 3, Name = "清次郎" },
            new Parameter() { ID = 2, Name = "誠三郎" },
            new Parameter() { ID = 5, Name = "征史郎" },
        };

        IOrderedEnumerable<Parameter>   orderedParameters   = parameters.OrderByDescending( value => value.ID );

        System.Console.WriteLine( "parameters        :{0}", parameters.Text() );
        System.Console.WriteLine( "parameters ordered:{0}", orderedParameters.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;
    }
}

parameters :[ID:0, Name:正一郎], [ID:3, Name:清次郎], [ID:2, Name:誠三郎], [ID:5, Name:征史郎],
parameters ordered:[ID:5, Name:征史郎], [ID:3, Name:清次郎], [ID:2, Name:誠三郎], [ID:0, Name:正一郎],

如果直接拿类进行比较呢?

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 override string ToString()
        {
            return string.Format( "ID:{0}, Name:{1}", ID, Name );
        }
    }

    static void Main( string[] args )
    {
        Parameter[] parameters = new Parameter[]
        {
            new Parameter() { ID = 0, Name = "正一郎" },
            new Parameter() { ID = 3, Name = "清次郎" },
            new Parameter() { ID = 2, Name = "誠三郎" },
            new Parameter() { ID = 5, Name = "征史郎" },
        };
        
        IOrderedEnumerable<Parameter>   orderedParameters   = parameters.OrderBy( value => value );

        try
        {
            bool isAny = orderedParameters.Any();
        }
        catch( System.Exception i_exception )
        {
            System.Console.WriteLine( "{0}", i_exception );
            System.Console.ReadKey();
            return;
        }
        
        System.Console.WriteLine( "parameters        :{0}", parameters.Text() );
        System.Console.WriteLine( "parameters ordered:{0}", orderedParameters.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必须在至少一个对象上实现。

直接用类比较会报错。但是我们可以通过继承IComparable接口实现。

using System.Linq;
using System.Collections;
using System.Collections.Generic;

public static class Program
{
    private class Parameter : System.IComparable<Parameter>
    {
        public int      ID      { get; set; }
        public string   Name    { get; set; }

        public int CompareTo( Parameter i_other )
        {
            if( i_other == null )
            {
                return -1;
            }

            return ID - i_other.ID;
        }

        public override string ToString()
        {
            return string.Format( "ID:{0}, Name:{1}", ID, Name );
        }
    }

    static void Main( string[] args )
    {
        Parameter[] parameters = new Parameter[]
        {
            new Parameter() { ID = 0, Name = "正一郎" },
            new Parameter() { ID = 3, Name = "清次郎" },
            new Parameter() { ID = 2, Name = "誠三郎" },
            new Parameter() { ID = 5, Name = "征史郎" },
        };

        IOrderedEnumerable<Parameter>   ascendingParameters     = parameters.OrderBy( value => value );
        IOrderedEnumerable<Parameter>   descendingParameters    = parameters.OrderByDescending( value => value );

        System.Console.WriteLine( "parameters     :{0}", parameters.Text() );
        System.Console.WriteLine( "parameters 昇順:{0}", ascendingParameters.Text() );
        System.Console.WriteLine( "parameters 降順:{0}", descendingParameters.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;
    }
} 


parameters :[ID:0, Name:正一郎], [ID:3, Name:清次郎], [ID:2, Name:誠三郎], [
ID:5, Name:征史郎],
parameters 昇順:[ID:0, Name:正一郎], [ID:2, Name:誠三郎], [ID:3, Name:清次郎], [
ID:5, Name:征史郎],
parameters 降順:[ID:5, Name:征史郎], [ID:3, Name:清次郎], [ID:2, Name:誠三郎], [
ID:0, Name:正一郎],

对于现有类型,我们也可以通过IComparer自定义排序规则。

using System.Linq;
using System.Collections;
using System.Collections.Generic;

public static class Program
{
    private class CompareInt : IComparer<int>
    {
        public int Compare( int i_lhs, int i_rhs )
        {
            bool isEvenNumberL  = i_lhs % 2 == 0;
            bool isEvenNumberR  = i_rhs % 2 == 0;

            if( isEvenNumberL && !isEvenNumberR )
            {
                return -1;
            }
            if( !isEvenNumberL && isEvenNumberR )
            {
                return 1;
            }
            return i_lhs - i_rhs;
        }
    }

    static void Main( string[] args )
    {
        int[]   numbers = new int[] { 3, 1, 2, 0, 4, 4, 7, 4, 8, 9 };

        CompareInt compare = new CompareInt();
        IOrderedEnumerable<int> ascendingNumbers    = numbers.OrderBy( value => value, compare );
        IOrderedEnumerable<int> descendingNumbers   = numbers.OrderByDescending( value => value, compare );

        System.Console.WriteLine( "numbers     :{0}", numbers.Text() );
        System.Console.WriteLine( "numbers 昇順:{0}", ascendingNumbers.Text() );
        System.Console.WriteLine( "numbers 降順:{0}", descendingNumbers.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;
    }
}



numbers :[3], [1], [2], [0], [4], [4], [7], [4], [8], [9],
numbers 昇順:[0], [2], [4], [4], [4], [8], [1], [3], [7], [9],
numbers 降順:[9], [7], [3], [1], [8], [4], [4], [4], [2], [0],

你可能感兴趣的:(C#,c#,.net,linq)