Ordinamento dei dati

Un'operazione di ordinamento dispone gli elementi di una sequenza in base a uno o più attributi. Il primo criterio di ordinamento esegue un ordinamento primario sugli elementi. Specificando un secondo criterio di ordinamento, è possibile ordinare gli elementi all'interno di ogni gruppo di ordinamento primario.

Nella figura seguente vengono illustrati i risultati di un'operazione di ordinamento alfabetico su una sequenza di caratteri.

Operazione di ordinamento LINQ

I metodi degli operatori di query standard che ordinano i dati sono riportati nella sezione seguente.

Metodi

Nome metodo

Descrizione

Sintassi dell'espressione di query in C#

Sintassi dell'espressione di query in Visual Basic

Ulteriori informazioni

OrderBy

Ordina i valori in ordine crescente.

orderby

Order By

Enumerable.OrderBy

Queryable.OrderBy

OrderByDescending

Ordina i valori in ordine decrescente.

orderby … descending

Order By … Descending

Enumerable.OrderByDescending

Queryable.OrderByDescending

ThenBy

Esegue un ordinamento secondario in ordine crescente.

orderby …, …

Order By …, …

Enumerable.ThenBy

Queryable.ThenBy

ThenByDescending

Esegue un ordinamento secondario in ordine decrescente.

orderby …, … descending

Order By …, … Descending

Enumerable.ThenByDescending

Queryable.ThenByDescending

Inverso

Inverte l'ordine degli elementi in un insieme.

Non applicabile.

Non applicabile.

Enumerable.Reverse<TSource>

Queryable.Reverse<TSource>

Esempi di sintassi dell'espressione di query

Esempi di ordinamento primario

Ordinamento primario crescente

Nell'esempio seguente viene illustrato come utilizzare la clausola orderby (Order By in Visual Basic) in una query LINQ per ordinare le stringhe in una matrice in base alla lunghezza della stringa, in ordine crescente.


        Dim words = {"the", "quick", "brown", "fox", "jumps"}

        Dim sortQuery = From word In words 
                        Order By word.Length 
                        Select word

        Dim sb As New System.Text.StringBuilder()
        For Each str As String In sortQuery
            sb.AppendLine(str)
        Next

        ' Display the results.
        MsgBox(sb.ToString())

        ' This code produces the following output:

        ' the
        ' fox
        ' quick
        ' brown
        ' jumps


            string[] words = { "the", "quick", "brown", "fox", "jumps" };

            IEnumerable<string> query = from word in words
                                        orderby word.Length
                                        select word;

            foreach (string str in query)
                Console.WriteLine(str);

            /* This code produces the following output:

                the
                fox
                quick
                brown
                jumps
            */

Ordinamento primario decrescente

Nell'esempio successivo viene illustrato come utilizzare la clausola orderby descending (Order By Descending in Visual Basic) in una query LINQ per ordinare le stringhe in base alla prima lettera, in ordine decrescente.


        Dim words = {"the", "quick", "brown", "fox", "jumps"}

        Dim sortQuery = From word In words 
                        Order By word.Substring(0, 1) Descending 
                        Select word

        Dim sb As New System.Text.StringBuilder()
        For Each str As String In sortQuery
            sb.AppendLine(str)
        Next

        ' Display the results.
        MsgBox(sb.ToString())

        ' This code produces the following output:

        ' the
        ' quick
        ' jumps
        ' fox
        ' brown


            string[] words = { "the", "quick", "brown", "fox", "jumps" };

            IEnumerable<string> query = from word in words
                                        orderby word.Substring(0, 1) descending
                                        select word;

            foreach (string str in query)
                Console.WriteLine(str);

            /* This code produces the following output:

                the
                quick
                jumps
                fox
                brown
            */

Esempi di ordinamento secondario

Ordinamento secondario crescente

Nell'esempio seguente viene illustrato come utilizzare la clausola orderby (Order By in Visual Basic) in una query LINQ per eseguire un ordinamento primario e secondario delle stringhe in una matrice. Le stringhe vengono ordinate prima in base alla lunghezza e poi in base alla prima lettera della stringa, in ordine crescente.


        Dim words = {"the", "quick", "brown", "fox", "jumps"}

        Dim sortQuery = From word In words 
                        Order By word.Length, word.Substring(0, 1) 
                        Select word

        Dim sb As New System.Text.StringBuilder()
        For Each str As String In sortQuery
            sb.AppendLine(str)
        Next

        ' Display the results.
        MsgBox(sb.ToString())

        ' This code produces the following output:

        ' fox
        ' the
        ' brown
        ' jumps
        ' quick


            string[] words = { "the", "quick", "brown", "fox", "jumps" };

            IEnumerable<string> query = from word in words
                                        orderby word.Length, word.Substring(0, 1)
                                        select word;

            foreach (string str in query)
                Console.WriteLine(str);

            /* This code produces the following output:

                fox
                the
                brown
                jumps
                quick
            */

Ordinamento secondario decrescente

Nell'esempio successivo viene illustrato come utilizzare la clausola orderby descending (Order By Descending in Visual Basic) in una query LINQ per eseguire un ordinamento primario in ordine crescente e un ordinamento secondario in ordine decrescente. Le stringhe vengono ordinate prima in base alla lunghezza e poi in base alla prima lettera della stringa.


        Dim words = {"the", "quick", "brown", "fox", "jumps"}

        Dim sortQuery = From word In words 
                        Order By word.Length, word.Substring(0, 1) Descending 
                        Select word

        Dim sb As New System.Text.StringBuilder()
        For Each str As String In sortQuery
            sb.AppendLine(str)
        Next

        ' Display the results.
        MsgBox(sb.ToString())

        ' This code produces the following output:

        ' fox
        ' the
        ' quick
        ' jumps
        ' brown


            string[] words = { "the", "quick", "brown", "fox", "jumps" };

            IEnumerable<string> query = from word in words
                                        orderby word.Length, word.Substring(0, 1) descending
                                        select word;

            foreach (string str in query)
                Console.WriteLine(str);

            /* This code produces the following output:

                the
                fox
                quick
                jumps
                brown
            */

Vedere anche

Attività

Procedura: ordinare i risultati di una clausola join (Guida per programmatori C#)

Procedura: ordinare i risultati di query utilizzando LINQ (Visual Basic)

Procedura: ordinare o filtrare i dati di testo in base a qualsiasi parola o campo (LINQ)

Riferimenti

Clausola orderby (Riferimento C#)

Clausola Order By (Visual Basic)

System.Linq

Concetti

Cenni preliminari sugli operatori di query standard