Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Sort example
//Rextester.Program.Main is the entry point for your code. Don't change it. //Compiler version 4.0.30319.17929 for Microsoft (R) .NET Framework 4.5 using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; namespace Rextester { public class Program { public static void Main(string[] args) { List<Test> tList = new List<Test> () { new Test("A", 1, 2), new Test("B", 5, 1), new Test("C", 0, 4), new Test("D", 5, 0), new Test("E", 0, 4) }; //Console.WriteLine(string.Join(",", tList.ConvertAll<string>(t => (t == null).ToString()))); /*tList.Sort((a, b) => { if ( a.x > b.x) { return 1; } else if (b.x > a.x) { return -1; } else { if ( a.y >= b.y) { return 1; } else { return -1; } } });*/ tList.Sort(); //Your code goes here Console.WriteLine(string.Join(",", tList.ConvertAll<string>(t => t.name))); } } public class Test : IComparable { public string name; public int x; public int y; public Test(string name, int x, int y) { this.name = name; this.x = x; this.y = y; } public int CompareTo(object obj) { if (obj == null) return 1; Test otherTest = obj as Test; if (otherTest != null) { int compare = x.CompareTo(otherTest.x); if ( compare == 0 ) { compare = y.CompareTo(otherTest.y); } return compare; } else { throw new ArgumentException("Object is not a Test"); } } } }
run
|
edit
|
history
|
help
0
Frase
Nullable type - Value Exception - Solution
sample code
e
Math 5.81
Arreglo
gg
Side 1
Hash autoryzujący
Programmes