To sort a set of elements that implement IComparable with Linq use OrderBy
var words = new[] { "Perl", "c#", "ruby", "java" }; var ordered = words .OrderBy(x => x); ordered.ToList().ForEach(Console.WriteLine);
Note that the default sort ignores case.
output:
c# java Perl ruby
The Ruby equivalent is sort
words = [ "Perl", "c#", "ruby", "java" ] ordered = words .sort puts ordered
Note that the default sort respects case.
output:
Perl c# java ruby
To sort a set of elements that do not implement IComparable, or based on a characteristic with Linq use OrderBy
var types = new[] { "c#".GetType(), true.GetType(), 1.GetType() }; var ordered = types .OrderBy(x => x.Name); ordered.ToList().ForEach(Console.WriteLine);
output:
System.Boolean System.Int32 System.String
The Ruby equivalents are sort
types = [ "c#".class, true.class, 1.class ] ordered = types .sort{|a,b| a.name <=> b.name} puts ordered
and sort_by
types = [ "c#".class, true.class, 1.class ] ordered = types .sort_by{|x| x.name} puts ordered
Both Ruby options output:
Fixnum String TrueClass
Linq methods:
All Any Count First FirstOrDefault GroupBy Max OrderBy Select Single
Skip SkipWhile Where