c# - How can I order this simple .NET list by two properties? -
Looking at the list of
location
's, I need 2 ordering steps.
- Order
Location type
ascending - The type of order for each location is the result of the
name
ascending order.
Sample data:
location type | name 2 | tempstowe lower 2 | tempstowe 1 | melbourne Expected Result: 1 melbourne 2. Templestowe 3. Templestowe low
...
And here is the main code (that has been copied from Bela) ..
Private class space {public location (string of Name, int location type) {name = name; LocationType = locationType;} public string Gm {get; private set;} Public Ink location type {get; private set;}} Public static zero main () {var location = new list & location> location & gt; {new location ("templosto lower", 2) , New location ("Templosto", 2), New location ("Melbourne", 1)}; // TODO: Sort by a list, location type and name. Var results = xxxxx; foreign currency (different locations in result } {Console.WriteLine (location.Name);}}
locations .Sort ((x, y) = & gt; {Int delta = x.LocationType.CompareTo (y.LocationType); If (delta == 0) delta = string Compar (x.Name, y.Name); Return delta;});
Alternatively:
var results = places. Orderbay (x = & gt; x.LocationType). Then (x = & x; x.Name);
or in the LINQ syntax (compiled for the same thing):
var results = loc.LocationType, loc.Name from place of location Choose ;
Comments
Post a Comment