Sorting an IList in C#

前端 未结 15 2243
臣服心动
臣服心动 2020-11-28 06:36

So I came across an interesting problem today. We have a WCF web service that returns an IList. Not really a big deal until I wanted to sort it.

Turns out the IList

相关标签:
15条回答
  • 2020-11-28 06:55
    using System.Linq;
    
    var yourList = SomeDAO.GetRandomThings();
    yourList.ToList().Sort( (thing, randomThing) => thing.CompareThisProperty.CompareTo( randomThing.CompareThisProperty ) );
    

    That's pretty !ghetto.

    0 讨论(0)
  • 2020-11-28 07:00

    This looks MUCH MORE SIMPLE if you ask me. This works PERFECTLY for me.

    You could use Cast() to change it to IList then use OrderBy():

        var ordered = theIList.Cast<T>().OrderBy(e => e);
    

    WHERE T is the type eg. Model.Employee or Plugin.ContactService.Shared.Contact

    Then you can use a for loop and its DONE.

      ObservableCollection<Plugin.ContactService.Shared.Contact> ContactItems= new ObservableCollection<Contact>();
    
        foreach (var item in ordered)
        {
           ContactItems.Add(item);
        }
    
    0 讨论(0)
  • 2020-11-28 07:01

    Convert your IList into List<T> or some other generic collection and then you can easily query/sort it using System.Linq namespace (it will supply bunch of extension methods)

    0 讨论(0)
  • 2020-11-28 07:02

    You're going to have to do something like that i think (convert it into a more concrete type).

    Maybe take it into a List of T rather than ArrayList, so that you get type safety and more options for how you implement the comparer.

    0 讨论(0)
  • 2020-11-28 07:04

    Found a good post on this and thought I'd share. Check it out HERE

    Basically.

    You can create the following class and IComparer Classes

    public class Widget {
        public string Name = string.Empty;
        public int Size = 0;
    
        public Widget(string name, int size) {
        this.Name = name;
        this.Size = size;
    }
    }
    
    public class WidgetNameSorter : IComparer<Widget> {
        public int Compare(Widget x, Widget y) {
            return x.Name.CompareTo(y.Name);
    }
    }
    
    public class WidgetSizeSorter : IComparer<Widget> {
        public int Compare(Widget x, Widget y) {
        return x.Size.CompareTo(y.Size);
    }
    }
    

    Then If you have an IList, you can sort it like this.

    List<Widget> widgets = new List<Widget>();
    widgets.Add(new Widget("Zeta", 6));
    widgets.Add(new Widget("Beta", 3));
    widgets.Add(new Widget("Alpha", 9));
    
    widgets.Sort(new WidgetNameSorter());
    widgets.Sort(new WidgetSizeSorter());
    

    But Checkout this site for more information... Check it out HERE

    0 讨论(0)
  • 2020-11-28 07:07

    Is this a valid solution?

            IList<string> ilist = new List<string>();
            ilist.Add("B");
            ilist.Add("A");
            ilist.Add("C");
    
            Console.WriteLine("IList");
            foreach (string val in ilist)
                Console.WriteLine(val);
            Console.WriteLine();
    
            List<string> list = (List<string>)ilist;
            list.Sort();
            Console.WriteLine("List");
            foreach (string val in list)
                Console.WriteLine(val);
            Console.WriteLine();
    
            list = null;
    
            Console.WriteLine("IList again");
            foreach (string val in ilist)
                Console.WriteLine(val);
            Console.WriteLine();
    

    The result was: IList B A C

    List A B C

    IList again A B C

    0 讨论(0)
提交回复
热议问题