PositiveGuy
PositiveGuy

Reputation: 47763

Sorting List leads to conversion 'void' error

Can't figure out why I get a void error with this attempt to sort my list. parameterList is a dictionary originally then I convert it to a generic list then try sorting. I want back the list as a generic list as you can see:

   List<KeyValuePair<string,string>> sortedList = parameterList.ToList().Sort((left, right) => left.Key.Equals(right.Key, StringComparison.Ordinal)
                                                    ? string.Compare(left.Value, right.Value, StringComparison.Ordinal) 
                                                    : string.Compare(left.Key, right.Key, StringComparison.Ordinal));

Error: "Cannot convert source type 'void' to target type List<System.Collections.Generic.KeyValuePair<string,string>>

What is this void it's talking about....??

UPDATED

The list sorting still won't sort, using any of the sugestions or my original sort code that I grabbed from http://oauth.googlecode.com/svn/code/csharp/OAuthBase.cs

So this is how I have it:

        Dictionary<string, string> authParamsNonNormalized = new Dictionary<string, string> {
                                                                                                  {Constants.OAuthConsumerKey, consumerKey},
                                                                                                  {Constants.OAuthSignatureMethodKey, methodType},
                                                                                                  {Constants.OAuthTimestampKey, timeStamp()},
                                                                                                  {Constants.OAuthTokenKey, accessToken},
                                                                                                   {Constants.OAuthNonceKey, nonce},
                                                                                                  {Constants.OAuthVersionKey, Constants.OAuthVersion}
                                                                                              };

Then I convert it to a Generic list with the ToList()t as this was an incoming dictionary to my method that contains this code:

List<KeyValuePair<string,string>> sortedParamList = parameterList.ToList();
            sortedParamList.OrderBy(p => p.Key, StringComparer.Ordinal)
                                    .ThenBy(p => p.Value, StringComparer.Ordinal).ToList();

when I check sortedParamList, it's still in the same order...nothing happened.

UPDATED:

crap, yea I screwed up the last one, here is the working code:

List<KeyValuePair<string, string>> sortedParamList = parameterList.OrderBy(p => p.Key, StringComparer.Ordinal)
                            .ThenBy(p => p.Value, StringComparer.Ordinal).ToList();

Upvotes: 2

Views: 2670

Answers (4)

Douglas
Douglas

Reputation: 54897

List<T>.Sort does not returned the sorted list; rather, it performs the sort “in-place”, altering the list instance on which it is called. You probably mean to use:

List<KeyValuePair<string, string>> sortedList = parameterList.ToList();
sortedList.Sort((left, right) => left.Key.Equals(right.Key, StringComparison.Ordinal)
    ? string.Compare(left.Value, right.Value, StringComparison.Ordinal)
    : string.Compare(left.Key, right.Key, StringComparison.Ordinal));

The following LINQ is semantically equivalent, but significantly clearer:

List<KeyValuePair<string, string>> sortedList =
    parameterList.OrderBy(p => p.Key, StringComparer.Ordinal)
                 .ThenBy(p => p.Value, StringComparer.Ordinal)
                 .ToList();

Upvotes: 4

Jalal
Jalal

Reputation: 6846

.Sort((left, right) => ...

the Sort method is void, and you can't put it inside a List<...>

Upvotes: 0

Terkel
Terkel

Reputation: 1575

The Sort method is an instance method of List and sorts the current instance, thus does not return a list.

You'll need to split up your code.

List<KeyValuePair<string,string>> sortedList = parameterList.ToList();
sortedList.Sort((left, right) => left.Key.Equals(right.Key, StringComparison.Ordinal)
                                     ? string.Compare(left.Value, right.Value, StringComparison.Ordinal) 
                                     : string.Compare(left.Key, right.Key, StringComparison.Ordinal));

Upvotes: 2

M.Babcock
M.Babcock

Reputation: 18965

All of the overloads of List<T>.Sort sort in place and don't return anything.

Perhaps IEnumerable<T>.OrderBy would be more appropriate for what you're trying to do.

Upvotes: 2

Related Questions