Domanda

I have seen this class that looks like this:

    /// <summary>
    /// Provides an internal structure to sort the query parameter
    /// </summary>
    protected class QueryParameter
    {
        public QueryParameter(string name, string value)
        {
            Name = name;
            Value = value;
        }

        public string Name { get; private set; }
        public string Value { get; private set; }
    }

    /// <summary>
    /// Comparer class used to perform the sorting of the query parameters
    /// </summary>
    protected class QueryParameterComparer : IComparer<QueryParameter>
    {
        public int Compare(QueryParameter x, QueryParameter y)
        {
            return x.Name == y.Name 
                ? string.Compare(x.Value, y.Value) 
                : string.Compare(x.Name, y.Name);
        }
    }

Then there is a call later in the code that does the sort:

parameters.Sort(new QueryParameterComparer());

which all works fine. I decided that it was a waste of time creating a QueryParameter class that only had name value and it would probably be better to use Dictionary. With the dictionary, rather than use the Sort(new QueryParameterComparer()); I figured I could just do this:

parameters.ToList().Sort((x, y) => x.Key == y.Key ? string.Compare(x.Value, y.Value) : string.Compare(x.Key, y.Key));

The code compiles fine, but I am unsure whether it is working because the list just seems to output in the same order it was put in. So, can anyone tell me if I am doing this correctly or if I am missing something simple?

Cheers /r3plica

È stato utile?

Soluzione

The List<T>.Sort method is not part of LINQ.

You can use OrderBy/ThenBy extension methods before calling ToList():

parameters = parameter.OrderBy(x => x.Key).ThenBy(x => x.Value).ToList();

Altri suggerimenti

From your code, I surmise that parameters is your dictionary, and you're calling

parameters.ToList().Sort(...);

and then carrying on using parameters.

ToList() creates a new list; you are then sorting this list and discarding it. You're not sorting parameters at all, and in fact you can't sort it because it's a dictionary.

What you need is something along the lines of

var parametersList = parameters.ToList();
parametersList.Sort(...);

where ... is the same sort as before.

You could also do

var parametersList = parameters.OrderBy(...).ToList();

which is a more LINQ-y way of doing things.

It may even be appropriate to just do e.g.

foreach(var kvp in parameters.OrderBy(...))

(or however you plan on using the sorted sequence) if you're using the sorted seqence more often than you're changing the dictionary (i.e. there's no point caching a sorted version because the original data changes a lot).


Another point to note - a dictionary can't contain duplicate keys, so there's no point checking x.Key == y.Key any more - you just need to sort via (x, y) => string.Compare(x.Key, y.Key)

I'd be careful here, though - by the look of it, the original code did support duplicate keys, so by switchnig to a dictionary you might be breaking something.

Dictionary are only equivalent to two hash map, and allow you to access to any alement (given the key) with costant time O(1) (because the make a lookup search on an hashtable).

So if you would order the elements because you intended to do a dicotomic search later, you do not need that you should use directly dictionary (or if you would query for both the value in dictionary you could use a couple of dictionary with the same elements but switching key value pairs).

As somebody write before me, if you question is how to order a list with linq you should work with linq and with orderby thenby.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top