score:12

Accepted answer

you should be able to mimick the signature of the orderby extension method:

update 1 you have to be explicit in the first generic parameter to your keyselector func. i'm going to take a guess at your type and call it "song".

public void sort<tkey>(sortorder sortorder,
                       func<song, tkey> keyselector)
{
    if (sortorder == sortorder.descending)
    {
        _list = _list.orderbydescending(keyselector).tolist(); 
    }
    else
    {
        _list = _list.orderby(keyselector).tolist(); 
    }
}

now you can call "sort" like this:

sort(sortorder.descending, x => x.album);

update 2

following up on tom lokhorst's comment: if you want to predefine some shorthand sort criteria, you could do so by defining a class like this:

public static class sortcolumn
{
    public static readonly func<song, string> artist = x => x.artist;
    public static readonly func<song, string> album = x => x.album;
}

now you can simply call:

sort(sortorder.descending, sortcolumn.artist);

score:-1

sounds like sorting is taking on a life of its own if you have several methods dedicated to it. maybe they can be gathered into a class.

public enum sortorder
{
    ascending = 0,
    descending = 1
}
public class sorter<t>
{
    public sortorder direction { get; set; }
    public func<t, object> target { get; set; }
    public sorter<t> nextsort { get; set; }

    public iorderedenumerable<t> applysorting(ienumerable<t> source)
    {
        iorderedenumerable<t> result = direction == sortorder.descending ?
            source.orderbydescending(target) : 
            source.orderby(target);

        if (nextsort != null)
        {
            result = nextsort.applynextsorting(result);
        }
        return result;
    }

    private iorderedenumerable<t> applynextsorting
        (iorderedenumerable<t> source)
    {
        iorderedenumerable<t> result = direction == sortorder.descending ?
            source.thenbydescending(target) :
            source.thenby(target);
        return result;
    }
}

here's sample usage:

list<string> source = new list<string>()
    { "john", "paul", "george", "ringo" };

sorter<string> mysorter = new sorter<string>()
{
    target = s => s.length,
    nextsort = new sorter<string>()
    {
        direction = sortorder.descending,
        target = s => s
    }
};

foreach (string s in mysorter.applysorting(source))
{
    console.writeline(s);
}

output is paul, john, ringo, george.

score:0

i think you should add an extension method to ilist<t>:

 public static class extilist {
    public static ilist<t> sort<t, tkey>(this ilist<t> list, sortorder sortorder, func<t, tkey> keyselector) {
            if (sortorder == sortorder.descending) {
                return list.orderbydescending(keyselector).tolist();
            } else {
                return list.orderby(keyselector).tolist();
            }
    }
}

and then you can use pretty with every your objects:

ilist<person> list = new list<person>();

list.add(new person("david","beckham"));
list.add(new person("gennaro","gattuso"));
list.add(new person("cristian","carlesso"));

list = list.sort(sortorder.descending, x => x.name);

ps sortorder already exists:

using system.data.sqlclient;

score:2

you might try using a generic comparer.


Related Query

More Query from same tag