1

I am trying to do a sort on dictionary

class Program {
    static void Main()
    {
        // Example dictionary.
        var dictionary = new Dictionary<string, int>(5);
        dictionary.Add("cat", 3);
        dictionary.Add("dog", 1);
        dictionary.Add("mouse", 0);
        dictionary.Add("elephant", 2);
        dictionary.Add("bird", 4);

        var items = from pair in dictionary
                orderby pair.Value ascending
                select pair;

        // Display results.
        foreach (KeyValuePair<string, int> pair in items)
        {
            Console.WriteLine("{0}: {1}", pair.Key, pair.Value);
        }
        items = from pair in dictionary
        orderby pair.Value descending
        select pair;
    } }

The result is

mouse
dog
elephant
cat
bird

But I need to exclude the first pair to sort the rest and to get this result

cat
mouse
dog
elephant
bird   

Can I do anything from here?

var items = from pair in dictionary
                orderby pair.Value ascending
                select pair;
Jonathan
  • 49
  • 1
  • 9
  • 3
    Are you sure, that a dictionary is the right collection type to use here? – scher Jul 01 '16 at 05:58
  • Your question with a dictionary does not make sense as it has no intrinsic order and therefore there is no "first pair". See http://stackoverflow.com/questions/4007782/the-order-of-elements-in-dictionary – DAXaholic Jul 01 '16 at 06:01
  • I too think your case it not proper for dictionary. There are sorted dictionary (and sortedlist and sortedset). And if you can detect what you would want to be first pair you could write your own orderby callback so it always compare that first key as higher order – Thaina Yu Jul 01 '16 at 06:05

6 Answers6

3

I would suggest using Linq

    var result = dictionary
        .Take(1)                        // Take first element
        .Concat(dictionary              // Skip first element and sort the rest on value. 
                .Skip(1)
                .OrderBy(o=>o.Value))
        .Select(x=>x.Key);

Output

   cat   ,
   mouse   ,
   dog   ,
   elephant   ,
   bird

Check this Demo

Hari Prasad
  • 16,716
  • 4
  • 21
  • 35
1

dictionary.Take(1).Concat(dictionary.Skip(1).OrderBy({ logic }))

You get the idea I think

Thaina Yu
  • 1,372
  • 2
  • 16
  • 27
0

Try something like this

var result = YourList.OrderBy(mc => mc.SomePropToSortOn)
                   .ToDictionary(mc => mc.Key.ToString(), 
                                 mc => mc.Value.ToString(), 
                                 StringComparer.OrdinalIgnoreCase);
MMM
  • 3,132
  • 3
  • 20
  • 32
0

You can use LINQ .OrderBy:

dictionary.OrderBy(t => t.Value).ToDictionary(t => t.Key, t=> t.Value);
0
var firstItems = dictionary.Take(1);
var sortItems = from pair in dictionary.Skip(1)
                orderby pair.Value ascending
                select pair;
var items = firstItems.Concat(sortItems);

Beware that Dictionary does not guarantee orders.

According its implementation, orders will be messed up once after item removal.

exclude the first pair

I would suggest not to note your item by orders.

Tommy
  • 3,044
  • 1
  • 14
  • 13
0

You can try to use this code:

var first = dictionary.Take(1);
var orderedRest = dictionary.Skip(1).OrderBy(p => p.Value);
var items = first.Union(orderedRest);

I already mentioniod that I think that a Dictionary is not the right collection type for this. In my opinion you should use List<KeyValuePair<string, int>>. The above code works with the list, too.

scher
  • 1,813
  • 2
  • 18
  • 39