Tuple<T1,T2,T3,T4,T5>.IComparable.CompareTo(Object) Metodo
Definizione
Importante
Alcune informazioni sono relative alla release non definitiva del prodotto, che potrebbe subire modifiche significative prima della release definitiva. Microsoft non riconosce alcuna garanzia, espressa o implicita, in merito alle informazioni qui fornite.
Confronta l'oggetto corrente Tuple<T1,T2,T3,T4,T5> con un oggetto specificato e restituisce un numero intero che indica se l'oggetto corrente è precedente, successivo o nella stessa posizione dell'oggetto specificato nell'ordinamento.
virtual int System.IComparable.CompareTo(System::Object ^ obj) = IComparable::CompareTo;
int IComparable.CompareTo(object obj);
abstract member System.IComparable.CompareTo : obj -> int
override this.System.IComparable.CompareTo : obj -> int
Function CompareTo (obj As Object) As Integer Implements IComparable.CompareTo
Parametri
- obj
- Object
Oggetto da confrontare con l'istanza corrente.
Valori restituiti
Intero con segno che indica la posizione relativa di questa istanza e obj nell'ordinamento, come illustrato nella tabella seguente.
| Valore | Descrizione |
|---|---|
| Intero negativo | Questa istanza precede obj.
|
| Zero | Questa istanza e obj ha la stessa posizione nell'ordinamento.
|
| Intero positivo | Questa istanza segue obj.
|
Implementazioni
Eccezioni
obj non è un Tuple<T1,T2,T3,T4,T5> oggetto .
Esempio
Nell'esempio seguente viene creata una matrice di Tuple<T1,T2,T3,T4,T5> oggetti che contengono statistiche di carriera per l'esecuzione di back nel football professionistico americano. I cinque componenti sono costituiti dal nome del giocatore, dal numero di partite in cui ha giocato, dal numero di corse o tentativi, dal numero totale di yard ottenute e dal numero di touchdown segnati. Nell'esempio vengono visualizzati i componenti di ogni tupla nella matrice in ordine non ordinato, ordina la matrice e quindi chiama ToString per visualizzare ogni tupla in ordine ordinato. L'output mostra che la matrice è stata ordinata in base al nome, ovvero il primo componente. Si noti che l'esempio non chiama direttamente il IComparable.CompareTo metodo . Questo metodo viene chiamato in modo implicito dal Sort(Array) metodo per ogni elemento nella matrice.
using System;
using System.Collections.Generic;
public class Example
{
public static void Main()
{
// Organization of runningBacks 5-tuple:
// Component 1: Player name
// Component 2: Number of games played
// Component 3: Number of attempts (carries)
// Component 4: Number of yards gained
// Component 5: Number of touchdowns
Tuple<string, int, int, int, int>[] runningBacks =
{ Tuple.Create("Payton, Walter", 190, 3838, 16726, 110),
Tuple.Create("Sanders, Barry", 153, 3062, 15269, 99),
Tuple.Create("Brown, Jim", 118, 2359, 12312, 106),
Tuple.Create("Dickerson, Eric", 144, 2996, 13259, 90),
Tuple.Create("Faulk, Marshall", 176, 2836, 12279, 100) };
// Display the array in unsorted order.
Console.WriteLine("The values in unsorted order:");
foreach (var runningBack in runningBacks)
Console.WriteLine(runningBack.ToString());
Console.WriteLine();
// Sort the array
Array.Sort(runningBacks);
// Display the array in sorted order.
Console.WriteLine("The values in sorted order:");
foreach (var runningBack in runningBacks)
Console.WriteLine(runningBack.ToString());
}
}
// The example displays the following output:
// The values in unsorted order:
// (Payton, Walter, 190, 3838, 16726, 110)
// (Sanders, Barry, 153, 3062, 15269, 99)
// (Brown, Jim, 118, 2359, 12312, 106)
// (Dickerson, Eric, 144, 2996, 13259, 90)
// (Faulk, Marshall, 176, 2836, 12279, 100)
//
// The values in sorted order:
// (Brown, Jim, 118, 2359, 12312, 106)
// (Dickerson, Eric, 144, 2996, 13259, 90)
// (Faulk, Marshall, 176, 2836, 12279, 100)
// (Payton, Walter, 190, 3838, 16726, 110)
// (Sanders, Barry, 153, 3062, 15269, 99)
open System
// Organization of runningBacks 5-tuple:
// Component 1: Player name
// Component 2: Number of games played
// Component 3: Number of attempts (carries)
// Component 4: Number of yards gained
// Component 5: Number of touchdowns
let runningBacks =
[| Tuple.Create("Payton, Walter", 190, 3838, 16726, 110)
Tuple.Create("Sanders, Barry", 153, 3062, 15269, 99)
Tuple.Create("Brown, Jim", 118, 2359, 12312, 106)
Tuple.Create("Dickerson, Eric", 144, 2996, 13259, 90)
Tuple.Create("Faulk, Marshall", 176, 2836, 12279, 100) |]
// Display the array in unsorted order.
printfn "The values in unsorted order:"
for runningBack in runningBacks do
printfn $"{runningBack}"
printfn ""
// Sort the array
Array.Sort runningBacks
// Display the array in sorted order.
printfn "The values in sorted order:"
for runningBack in runningBacks do
printfn $"{runningBack}"
// The example displays the following output:
// The values in unsorted order:
// (Payton, Walter, 190, 3838, 16726, 110)
// (Sanders, Barry, 153, 3062, 15269, 99)
// (Brown, Jim, 118, 2359, 12312, 106)
// (Dickerson, Eric, 144, 2996, 13259, 90)
// (Faulk, Marshall, 176, 2836, 12279, 100)
//
// The values in sorted order:
// (Brown, Jim, 118, 2359, 12312, 106)
// (Dickerson, Eric, 144, 2996, 13259, 90)
// (Faulk, Marshall, 176, 2836, 12279, 100)
// (Payton, Walter, 190, 3838, 16726, 110)
// (Sanders, Barry, 153, 3062, 15269, 99)
Imports System.Collections.Generic
Module Example
Public Sub Main()
' Organization of runningBacks 5-tuple:
' Component 1: Player name
' Component 2: Number of games played
' Component 3: Number of attempts (carries)
' Component 4: Number of yards gained
' Component 5: Number of touchdowns
Dim runningBacks() =
{ Tuple.Create("Payton, Walter", 190, 3838, 16726, 110),
Tuple.Create("Sanders, Barry", 153, 3062, 15269, 99),
Tuple.Create("Brown, Jim", 118, 2359, 12312, 106),
Tuple.Create("Dickerson, Eric", 144, 2996, 13259, 90),
Tuple.Create("Faulk, Marshall", 176, 2836, 12279, 100) }
' Display the array in unsorted order.
Console.WriteLine("The values in unsorted order:")
For Each runningBack In runningBacks
Console.WriteLine(runningBack.ToString())
Next
Console.WriteLine()
' Sort the array
Array.Sort(runningBacks)
' Display the array in sorted order.
Console.WriteLine("The values in sorted order:")
For Each runningBack In runningBacks
Console.WriteLine(runningBack.ToString())
Next
End Sub
End Module
' The example displays the following output:
' The values in unsorted order:
' (Payton, Walter, 190, 3838, 16726, 110)
' (Sanders, Barry, 153, 3062, 15269, 99)
' (Brown, Jim, 118, 2359, 12312, 106)
' (Dickerson, Eric, 144, 2996, 13259, 90)
' (Faulk, Marshall, 176, 2836, 12279, 100)
'
' The values in sorted order:
' (Brown, Jim, 118, 2359, 12312, 106)
' (Dickerson, Eric, 144, 2996, 13259, 90)
' (Faulk, Marshall, 176, 2836, 12279, 100)
' (Payton, Walter, 190, 3838, 16726, 110)
' (Sanders, Barry, 153, 3062, 15269, 99)
Commenti
Questo membro è un'implementazione esplicita del membro dell'interfaccia. Può essere usato solo quando viene eseguito il cast dell'istanza a un'interfaccia Tuple<T1,T2,T3,T4,T5>IComparable .
Questo metodo fornisce l'implementazione IComparable.CompareTo per la Tuple<T1,T2,T3,T4,T5> classe . Anche se il metodo può essere chiamato direttamente, viene chiamato più comunemente dagli overload predefiniti dei metodi di ordinamento delle raccolte, ad esempio Array.Sort(Array) e SortedList.Add, per ordinare i membri di una raccolta.
Attenzione
Il IComparable.CompareTo metodo è destinato all'uso nelle operazioni di ordinamento. Non deve essere usato quando lo scopo principale di un confronto è determinare se due oggetti sono uguali. Per determinare se due oggetti sono uguali, chiamare il Tuple<T1,T2,T3,T4,T5>.Equals(Object) metodo .
Il IComparable.CompareTo(Object) metodo usa l'operatore di confronto dell'oggetto predefinito per confrontare ogni componente.