Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jianminchen/eff03bea08a95061deb4185af74fea18 to your computer and use it in GitHub Desktop.
Save jianminchen/eff03bea08a95061deb4185af74fea18 to your computer and use it in GitHub Desktop.
C Sharpt - Dictionary - OrderByDescending and distinct method - Baby step to try out methods of Dictionary class, learn API
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DictionaryOrderBy
{
class Program
{
static void Main(string[] args)
{
tryOrderByDescending();
tryDistinct();
}
/*
* Dictionary class OrderByDescending
*
* June 9, 2016
*/
public static void tryOrderByDescending()
{
Dictionary<int, string> d = new Dictionary<int, string>();
d.Add(5, "Foo5");
d.Add(1, "Foo1");
d.Add(3, "Foo3");
var result = d.OrderByDescending(i => i.Key);
var result2 = d.OrderByDescending(i => i.Value);
string s = "";
foreach (KeyValuePair<int, string> runner in result2)
{
s += runner.Key.ToString() + "=" + runner.Value.ToString() + " ";
}
//or
IEnumerable<KeyValuePair<int, string>> result1 = d.OrderByDescending(i => i.Key);
foreach (KeyValuePair<int, string> kvp in result)
{
Console.WriteLine(kvp.Value);
}
Console.WriteLine("Second Option");
foreach (KeyValuePair<int, string> kvp in result1)
{
Console.WriteLine(kvp.Value);
}
}
/*
* Dictionary Class Distinct
*
* June 10, 2016
*/
public static void tryDistinct()
{
List<int> ages = new List<int> { 21, 46, 46, 55, 17, 21, 55, 55 };
IEnumerable<int> distinctAges = ages.Distinct();
Console.WriteLine("Distinct ages:");
foreach (int age in distinctAges)
{
Console.WriteLine(age);
}
/*
This code produces the following output:
Distinct ages:
21
46
55
17
*/
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment