1

I have created a custom list class that is not IEnumerable. Therefore, I can't use Enumerable.OrderBy.

class MyList<T> 
{
    class ListElement
    {
        public T content;
        public ListElement next;
    }
    ListElement head;
}

After this I put objects in it that have a string property. I want to sort the list by the string property of the objects it contains.

PiotrWolkowski
  • 8,408
  • 6
  • 48
  • 68

2 Answers2

0

In this case it's best just to use IEnumerable, or even better, just derive from the list class... I'll show you how you can achieve both.

public class MyList<T> : IEnumerable<T>
{
    private List<T> itemCollection;

    public MyList()
    {
        this.itemCollection = null;
    }

    public void Add(T item)
    {
        this.itemCollection.Add(item);
    }


    public IEnumerator<T> GetEnumerator()
    {
        foreach (T item in this.itemCollection)
        {
            yield return item;
        }
    }

    /// <summary>
    /// Returns an enumerator that iterates through a collection.
    /// </summary>
    /// <returns>
    /// An <see cref="T:System.Collections.IEnumerator"/> object that can be used to iterate through the collection.
    /// </returns>
    IEnumerator IEnumerable.GetEnumerator()
    {
        return this.GetEnumerator();
    }
}

Or the easiest of all...

public class MyList<T> : List<T>
{

}
Aydin
  • 15,016
  • 4
  • 32
  • 42
0

one option would be not using linked list, but System.Collections.Generic.List<T>

another would be implementing System.Collections.Generic.IEnumerable<T> and constructor from System.Collections.Generic.IEnumerable<T> on your linked list, like this:

using System;
using System.Collections.Generic;
using System.Linq;
class MyList<T> : IEnumerable<T> {
    class ListElement {
        public T content;
        public ListElement next;
    }
    ListElement head = null;

    public MyList() {
        head = null;
    }
    public MyList(IEnumerable<T> values)
        : this() {
        ListElement last = null;
        foreach(var v in values) {
            ListElement that = new ListElement { content = v };
            if(last != null) {
                last.next = that;
            } else {
                head = that;
            }
            last = that;
        }
    }

    public IEnumerator<T> GetEnumerator() {
        var current = head;
        while(current != null) {
            yield return current.content;
            current = current.next;
        }
    }
    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() {
        return this.GetEnumerator();
    }

then,

    public MyList<T> sortedBy<TK>(Func<T, TK> key) {
        return new MyList<T>(this.OrderBy(key));
    }
}

and

var sortedLinkedList = unsortedLinkedList.sortedBy(item => item.Property);

if you want sorting to mutate your list, not make new; or actually sort linked list, not convert to array and sort that (IEnumerable.OrderBy seems to do that), What's the fastest algorithm for sorting a linked list? might be of some use

Community
  • 1
  • 1
Jordan Szubert
  • 176
  • 1
  • 1
  • 4