Re: sort list base on another list

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Marc Gravell

    Re: sort list base on another list

    To illustrate Peter's point - here is the code (using C# 3 for
    brevity, but only .NET 2.0 - no LINQ etc) to do this using an entity
    (SomeType) to represent the combined values:

    Marc

    using System;
    using System.Collecti ons.Generic;

    sealed class SomeType
    {
    // for C# 2, could add fields and implement properties manually
    public string Foo { get; set; }
    public int Bar { get; set; }
    }
    static class Program
    {
    static void Main()
    {
    List<SomeTypeli st = new List<SomeType>
    { // for C# 2, could use a specific .ctor
    new SomeType {Foo="c", Bar=3},
    new SomeType {Foo="b", Bar=9},
    new SomeType {Foo="a", Bar=2}
    };
    // for C# 2, could be an anonymous method for comparison
    list.Sort((x,y) =string.Compare (x.Foo, y.Foo));
    foreach (SomeType item in list)
    {
    Console.WriteLi ne("{0}: {1}", item.Foo, item.Bar);
    }
    }
    }
Working...