Created
June 10, 2016 17:35
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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