Похожие чаты

Can someone explain what the heck is this? very weird

sort
sportList.Sort((a, b) => { return a.Result - b.Result; });

5 ответов

27 просмотров

standard sort... just providing an elements comparison callback that determine whether a is greater, equal, or lesser than b (callback result must be > 0, = 0 or < 0)

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 */ } }

Ray Golden
using System; using System.Collections.Generic; //...

Don't post big chunks of code like this. Either use pastebin or something, or attach it as a document. I will remove this post soon

Похожие вопросы

Обсуждают сегодня

Господа, а что сейчас вообще с рынком труда на делфи происходит? Какова ситуация?
Rꙮman Yankꙮvsky
29
А вообще, что может смущать в самой Julia - бы сказал, что нет единого стандартного подхода по многим моментам, поэтому многое выглядит как "хаки" и произвол. Короче говоря, с...
Viktor G.
2
@Benzenoid can you tell me the easiest, and safest way to bu.y HEX now?
Živa Žena
20
This is a question from my wife who make a fortune with memes 😂😂 About the Migration and Tokens: 1. How will the old tokens be migrated to the new $LGCYX network? What is th...
🍿 °anton°
2
30500 за редактор? )
Владимир
47
а через ESC-код ?
Alexey Kulakov
29
What is the Dex situation? Agora team started with the Pnetwork for their dex which helped them both with integration. It’s completed but as you can see from the Pnetwork ann...
Ben
1
Гайс, вопрос для разносторонее развитых: читаю стрим с юарта, нада выделять с него фреймы с определенной структурой, если ли чо готовое, или долбаться с ринг буффером? нада у...
Vitaly
9
Anyone knows where there are some instructions or discort about failed bridge transactions ?
Jochem
21
@lozuk how do I get my phex copies of my ehex from a atomic wallet, to move to my rabby?
Justfrontin 👀
11
Карта сайта