本文整理匯總了C#中System.Collections.Generic.List<T>.Sort方法的典型用法代碼示例。如果您正苦於以下問題:C# List<T>.Sort方法的具體用法?C# List<T>.Sort怎麽用?C# List<T>.Sort使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類System.Collections.Generic.List<T>
的用法示例。
在下文中一共展示了List<T>.Sort方法的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。
示例1: ToString
//引入命名空間
using System;
using System.Collections.Generic;
// Simple business object. A PartId is used to identify the type of part
// but the part name can change.
public class Part : IEquatable<Part> , IComparable<Part>
{
public string PartName { get; set; }
public int PartId { get; set; }
public override string ToString()
{
return "ID: " + PartId + " Name: " + PartName;
}
public override bool Equals(object obj)
{
if (obj == null) return false;
Part objAsPart = obj as Part;
if (objAsPart == null) return false;
else return Equals(objAsPart);
}
public int SortByNameAscending(string name1, string name2)
{
return name1.CompareTo(name2);
}
// Default comparer for Part type.
public int CompareTo(Part comparePart)
{
// A null value means that this object is greater.
if (comparePart == null)
return 1;
else
return this.PartId.CompareTo(comparePart.PartId);
}
public override int GetHashCode()
{
return PartId;
}
public bool Equals(Part other)
{
if (other == null) return false;
return (this.PartId.Equals(other.PartId));
}
// Should also override == and != operators.
}
public class Example
{
public static void Main()
{
// Create a list of parts.
List<Part> parts = new List<Part>();
// Add parts to the list.
parts.Add(new Part() { PartName = "regular seat", PartId = 1434 });
parts.Add(new Part() { PartName= "crank arm", PartId = 1234 });
parts.Add(new Part() { PartName = "shift lever", PartId = 1634 }); ;
// Name intentionally left null.
parts.Add(new Part() { PartId = 1334 });
parts.Add(new Part() { PartName = "banana seat", PartId = 1444 });
parts.Add(new Part() { PartName = "cassette", PartId = 1534 });
// Write out the parts in the list. This will call the overridden
// ToString method in the Part class.
Console.WriteLine("\nBefore sort:");
foreach (Part aPart in parts)
{
Console.WriteLine(aPart);
}
// Call Sort on the list. This will use the
// default comparer, which is the Compare method
// implemented on Part.
parts.Sort();
Console.WriteLine("\nAfter sort by part number:");
foreach (Part aPart in parts)
{
Console.WriteLine(aPart);
}
// This shows calling the Sort(Comparison(T) overload using
// an anonymous method for the Comparison delegate.
// This method treats null as the lesser of two values.
parts.Sort(delegate(Part x, Part y)
{
if (x.PartName == null && y.PartName == null) return 0;
else if (x.PartName == null) return -1;
else if (y.PartName == null) return 1;
else return x.PartName.CompareTo(y.PartName);
});
Console.WriteLine("\nAfter sort by name:");
foreach (Part aPart in parts)
{
Console.WriteLine(aPart);
}
/*
Before sort:
ID: 1434 Name: regular seat
ID: 1234 Name: crank arm
ID: 1634 Name: shift lever
ID: 1334 Name:
ID: 1444 Name: banana seat
ID: 1534 Name: cassette
After sort by part number:
ID: 1234 Name: crank arm
ID: 1334 Name:
ID: 1434 Name: regular seat
ID: 1444 Name: banana seat
ID: 1534 Name: cassette
ID: 1634 Name: shift lever
After sort by name:
ID: 1334 Name:
ID: 1444 Name: banana seat
ID: 1534 Name: cassette
ID: 1234 Name: crank arm
ID: 1434 Name: regular seat
ID: 1634 Name: shift lever
*/
}
}
示例2: CompareDinosByLength
//引入命名空間
using System;
using System.Collections.Generic;
public class Example
{
private static int CompareDinosByLength(string x, string y)
{
if (x == null)
{
if (y == null)
{
// If x is null and y is null, they're
// equal.
return 0;
}
else
{
// If x is null and y is not null, y
// is greater.
return -1;
}
}
else
{
// If x is not null...
//
if (y == null)
// ...and y is null, x is greater.
{
return 1;
}
else
{
// ...and y is not null, compare the
// lengths of the two strings.
//
int retval = x.Length.CompareTo(y.Length);
if (retval != 0)
{
// If the strings are not of equal length,
// the longer string is greater.
//
return retval;
}
else
{
// If the strings are of equal length,
// sort them with ordinary string comparison.
//
return x.CompareTo(y);
}
}
}
}
public static void Main()
{
List<string> dinosaurs = new List<string>();
dinosaurs.Add("Pachycephalosaurus");
dinosaurs.Add("Amargasaurus");
dinosaurs.Add("");
dinosaurs.Add(null);
dinosaurs.Add("Mamenchisaurus");
dinosaurs.Add("Deinonychus");
Display(dinosaurs);
Console.WriteLine("\nSort with generic Comparison<string> delegate:");
dinosaurs.Sort(CompareDinosByLength);
Display(dinosaurs);
}
private static void Display(List<string> list)
{
Console.WriteLine();
foreach( string s in list )
{
if (s == null)
Console.WriteLine("(null)");
else
Console.WriteLine("\"{0}\"", s);
}
}
}
輸出:
"Pachycephalosaurus" "Amargasaurus" "" (null) "Mamenchisaurus" "Deinonychus" Sort with generic Comparisondelegate: (null) "" "Deinonychus" "Amargasaurus" "Mamenchisaurus" "Pachycephalosaurus"
示例3: Compare
//引入命名空間
using System;
using System.Collections.Generic;
public class DinoComparer: IComparer<string>
{
public int Compare(string x, string y)
{
if (x == null)
{
if (y == null)
{
// If x is null and y is null, they're
// equal.
return 0;
}
else
{
// If x is null and y is not null, y
// is greater.
return -1;
}
}
else
{
// If x is not null...
//
if (y == null)
// ...and y is null, x is greater.
{
return 1;
}
else
{
// ...and y is not null, compare the
// lengths of the two strings.
//
int retval = x.Length.CompareTo(y.Length);
if (retval != 0)
{
// If the strings are not of equal length,
// the longer string is greater.
//
return retval;
}
else
{
// If the strings are of equal length,
// sort them with ordinary string comparison.
//
return x.CompareTo(y);
}
}
}
}
}
public class Example
{
public static void Main()
{
List<string> dinosaurs = new List<string>();
dinosaurs.Add("Pachycephalosaurus");
dinosaurs.Add("Parasauralophus");
dinosaurs.Add("Amargasaurus");
dinosaurs.Add("Galimimus");
dinosaurs.Add("Mamenchisaurus");
dinosaurs.Add("Deinonychus");
dinosaurs.Add("Oviraptor");
dinosaurs.Add("Tyrannosaurus");
int herbivores = 5;
Display(dinosaurs);
DinoComparer dc = new DinoComparer();
Console.WriteLine("\nSort a range with the alternate comparer:");
dinosaurs.Sort(0, herbivores, dc);
Display(dinosaurs);
Console.WriteLine("\nBinarySearch a range and Insert \"{0}\":",
"Brachiosaurus");
int index = dinosaurs.BinarySearch(0, herbivores, "Brachiosaurus", dc);
if (index < 0)
{
dinosaurs.Insert(~index, "Brachiosaurus");
herbivores++;
}
Display(dinosaurs);
}
private static void Display(List<string> list)
{
Console.WriteLine();
foreach( string s in list )
{
Console.WriteLine(s);
}
}
}
輸出:
Pachycephalosaurus Parasauralophus Amargasaurus Galimimus Mamenchisaurus Deinonychus Oviraptor Tyrannosaurus Sort a range with the alternate comparer: Galimimus Amargasaurus Mamenchisaurus Parasauralophus Pachycephalosaurus Deinonychus Oviraptor Tyrannosaurus BinarySearch a range and Insert "Brachiosaurus": Galimimus Amargasaurus Brachiosaurus Mamenchisaurus Parasauralophus Pachycephalosaurus Deinonychus Oviraptor Tyrannosaurus
示例4: foreach
String[] names = { "Samuel", "Dakota", "Koani", "Saya", "Vanya", "Jody",
"Yiska", "Yuma", "Jody", "Nikita" };
var nameList = new List<String>();
nameList.AddRange(names);
Console.WriteLine("List in unsorted order: ");
foreach (var name in nameList)
Console.Write(" {0}", name);
Console.WriteLine(Environment.NewLine);
nameList.Sort();
Console.WriteLine("List in sorted order: ");
foreach (var name in nameList)
Console.Write(" {0}", name);
Console.WriteLine();
輸出:
List in unsorted order: Samuel Dakota Koani Saya Vanya Jody Yiska Yuma Jody Nikita List in sorted order: Dakota Jody Jody Koani Nikita Samuel Saya Vanya Yiska Yuma
示例5: foreach
List<string> dinosaurs = new List<string>();
dinosaurs.Add("Pachycephalosaurus");
dinosaurs.Add("Amargasaurus");
dinosaurs.Add("Mamenchisaurus");
dinosaurs.Add("Deinonychus");
Console.WriteLine("Initial list:");
Console.WriteLine();
foreach(string dinosaur in dinosaurs)
{
Console.WriteLine(dinosaur);
}
Console.WriteLine("\nSort:");
dinosaurs.Sort();
Console.WriteLine();
foreach(string dinosaur in dinosaurs)
{
Console.WriteLine(dinosaur);
}
Console.WriteLine("\nBinarySearch and Insert \"Coelophysis\":");
int index = dinosaurs.BinarySearch("Coelophysis");
if (index < 0)
{
dinosaurs.Insert(~index, "Coelophysis");
}
Console.WriteLine();
foreach(string dinosaur in dinosaurs)
{
Console.WriteLine(dinosaur);
}
Console.WriteLine("\nBinarySearch and Insert \"Tyrannosaurus\":");
index = dinosaurs.BinarySearch("Tyrannosaurus");
if (index < 0)
{
dinosaurs.Insert(~index, "Tyrannosaurus");
}
Console.WriteLine();
foreach(string dinosaur in dinosaurs)
{
Console.WriteLine(dinosaur);
}
輸出:
Initial list: Pachycephalosaurus Amargasaurus Mamenchisaurus Deinonychus Sort: Amargasaurus Deinonychus Mamenchisaurus Pachycephalosaurus BinarySearch and Insert "Coelophysis": Amargasaurus Coelophysis Deinonychus Mamenchisaurus Pachycephalosaurus BinarySearch and Insert "Tyrannosaurus": Amargasaurus Coelophysis Deinonychus Mamenchisaurus Pachycephalosaurus Tyrannosaurus
示例6: Compare
//引入命名空間
using System;
using System.Collections.Generic;
public class DinoComparer: IComparer<string>
{
public int Compare(string x, string y)
{
if (x == null)
{
if (y == null)
{
// If x is null and y is null, they're
// equal.
return 0;
}
else
{
// If x is null and y is not null, y
// is greater.
return -1;
}
}
else
{
// If x is not null...
//
if (y == null)
// ...and y is null, x is greater.
{
return 1;
}
else
{
// ...and y is not null, compare the
// lengths of the two strings.
//
int retval = x.Length.CompareTo(y.Length);
if (retval != 0)
{
// If the strings are not of equal length,
// the longer string is greater.
//
return retval;
}
else
{
// If the strings are of equal length,
// sort them with ordinary string comparison.
//
return x.CompareTo(y);
}
}
}
}
}
public class Example
{
public static void Main()
{
List<string> dinosaurs = new List<string>();
dinosaurs.Add("Pachycephalosaurus");
dinosaurs.Add("Amargasaurus");
dinosaurs.Add("Mamenchisaurus");
dinosaurs.Add("Deinonychus");
Display(dinosaurs);
DinoComparer dc = new DinoComparer();
Console.WriteLine("\nSort with alternate comparer:");
dinosaurs.Sort(dc);
Display(dinosaurs);
SearchAndInsert(dinosaurs, "Coelophysis", dc);
Display(dinosaurs);
SearchAndInsert(dinosaurs, "Oviraptor", dc);
Display(dinosaurs);
SearchAndInsert(dinosaurs, "Tyrannosaur", dc);
Display(dinosaurs);
SearchAndInsert(dinosaurs, null, dc);
Display(dinosaurs);
}
private static void SearchAndInsert(List<string> list,
string insert, DinoComparer dc)
{
Console.WriteLine("\nBinarySearch and Insert \"{0}\":", insert);
int index = list.BinarySearch(insert, dc);
if (index < 0)
{
list.Insert(~index, insert);
}
}
private static void Display(List<string> list)
{
Console.WriteLine();
foreach( string s in list )
{
Console.WriteLine(s);
}
}
}
輸出:
Pachycephalosaurus Amargasaurus Mamenchisaurus Deinonychus Sort with alternate comparer: Deinonychus Amargasaurus Mamenchisaurus Pachycephalosaurus BinarySearch and Insert "Coelophysis": Coelophysis Deinonychus Amargasaurus Mamenchisaurus Pachycephalosaurus BinarySearch and Insert "Oviraptor": Oviraptor Coelophysis Deinonychus Amargasaurus Mamenchisaurus Pachycephalosaurus BinarySearch and Insert "Tyrannosaur": Oviraptor Coelophysis Deinonychus Tyrannosaur Amargasaurus Mamenchisaurus Pachycephalosaurus BinarySearch and Insert "": Oviraptor Coelophysis Deinonychus Tyrannosaur Amargasaurus Mamenchisaurus Pachycephalosaurus