本文整理汇总了C#中Dictionary.OrderBy方法的典型用法代码示例。如果您正苦于以下问题:C# Dictionary.OrderBy方法的具体用法?C# Dictionary.OrderBy怎么用?C# Dictionary.OrderBy使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Dictionary
的用法示例。
在下文中一共展示了Dictionary.OrderBy方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
static void Main(string[] args)
{
var lectures = new Dictionary<string, Tuple<int, int>>();
int count = int.Parse(Console.ReadLine().Split(' ')[1]);
for (int i = 0; i < count; i++)
{
var tokens = Console.ReadLine().Split(new[] { ' ', '-', ':' }, StringSplitOptions.RemoveEmptyEntries);
lectures.Add(tokens[0], new Tuple<int, int>(int.Parse(tokens[1]), int.Parse(tokens[2])));
}
int resCount = 0;
int earliestFinish = 0;
var sortedLectures = lectures.OrderBy(l => l.Value.Item2);
foreach (var lecture in sortedLectures)
{
if (lecture.Value.Item1 >= earliestFinish)
{
earliestFinish = lecture.Value.Item2;
resCount++;
continue;
}
lectures.Remove(lecture.Key);
}
Console.WriteLine($"Lectures ({resCount}):");
foreach (var lecture in lectures.OrderBy(l => l.Value.Item2))
{
Console.WriteLine($"{lecture.Value.Item1}-{lecture.Value.Item2} -> {lecture.Key}");
}
}
示例2: Encode
public string Encode() {
var values = new Dictionary<string, string>();
values.Add("bank_type", "WX");
values.Add("body", Body);
if (!string.IsNullOrEmpty(Attach))
values.Add("attach", Attach);
values.Add("partner", WxPayConfig.PARTER_ID);
values.Add("out_trade_no", OutTradeNo);
values.Add("total_fee", TotalFee.ToString());
values.Add("fee_type", FeeType.ToString());
values.Add("notify_url", string.IsNullOrEmpty(NotifyUrl)?WxPayConfig.NOTIFY_URL:NotifyUrl);
values.Add("spbill_create_ip", SPBill_Create_IP);
if (!string.IsNullOrEmpty(Time_Start))
values.Add("time_start", Time_Start);
if (!string.IsNullOrEmpty(Time_End))
values.Add("time_expire", Time_End);
values.Add("transport_fee", TransportFee.ToString());
values.Add("product_fee", ProductFee.ToString());
if (!string.IsNullOrEmpty(GoodsTag))
values.Add("goods_tag", GoodsTag);
values.Add("input_charset", InputCharset);
var signingStr = values.OrderBy(b => b.Key).Aggregate(new StringBuilder(), (s, b) => s.AppendFormat("{0}={1}&", b.Key, b.Value), s => s.ToString()).TrimEnd('&');
signingStr = string.Format("{0}&key={1}", signingStr, WxPayConfig.PARTER_KEY);
var signedStr = Util.MD5_Encode(signingStr).ToUpper();
var resultStr = values.OrderBy(b => b.Key).Aggregate(new StringBuilder(), (s, b) => s.AppendFormat("{0}={1}&", b.Key,Util.UrlEncode(b.Value)), s => s.ToString()).TrimEnd('&');
return string.Format("{0}&sign={1}", resultStr, signedStr);
}
示例3: Distance
public static string Distance()
{
Dictionary<string, double> distance;
distance = new Dictionary<string,double>();
distance.Add("normal", CalculateDistance(0, 0));
distance.Add("up", CalculateDistance(0, -1));
distance.Add("down", CalculateDistance(0, 1));
distance.Add("left", CalculateDistance(-1, 0));
distance.Add("right", CalculateDistance(1, 0));
Console.WriteLine(distance.OrderBy(kvp => kvp.Value).First().Key);
return distance.OrderBy(kvp => kvp.Value).First().Key;
}
示例4: GetRanked
public static int[] GetRanked(int[] A, int ABegin, int ALength)
{
Dictionary<int, int> Dict = new Dictionary<int, int>();
for (int i = 0; i < ALength; i++) Dict.Add(i, A[i + ABegin]);
Dictionary<int, int> Dict2 = new Dictionary<int, int>();
int index = 0;
foreach (var item in Dict.OrderBy(pair => pair.Value)) Dict[item.Key] = index++;
int[] ret = new int[ALength];
foreach (var item in Dict.OrderBy(pair => pair.Key))
{
ret[item.Key] = item.Value;
}
return ret;
}
示例5: Main
private static void Main(string[] args)
{
var reader = new StreamReader("..//..//text.txt", Encoding.UTF8);
string[] words;
using (reader)
{
words =
reader.ReadToEnd()
.Split(new[] { ' ', '.', '!', '?', '-', ',', '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries)
.Select(x => x.ToLower())
.ToArray();
}
var wordsDict = new Dictionary<string, int>();
for (int i = 0; i < words.Length; i++)
{
string word = words[i];
if (!wordsDict.ContainsKey(word))
{
wordsDict[word] = 0;
}
wordsDict[word]++;
}
var sortedWords = wordsDict.OrderBy(x => x.Value);
foreach (var i in sortedWords)
{
Console.WriteLine(i.Key + " -> " + i.Value + " times");
}
}
示例6: CountWordsFromFile
private static void CountWordsFromFile(string line)
{
var words = line.Trim().Split(new[] { ' ', '-', '.', ',', '!', '?' });
var meanWords = new Dictionary<string, int>();
foreach (var word in words)
{
if (word != string.Empty && word != "?" && word != "!" && word != "." && word != ",")
{
var lowerWord = word.ToLower();
if (meanWords.ContainsKey(lowerWord))
{
meanWords[lowerWord]++;
}
else
{
meanWords.Add(lowerWord, 1);
}
}
}
var dict = meanWords.OrderBy(x => x.Value);
foreach (var word in dict)
{
Console.WriteLine("{0} -> {1} times", word.Key, word.Value);
}
}
示例7: Main
static void Main()
{
string input = "prela baba tri 4%^&!nedeli, ta izprela tri kadeli!";
string[] words = Regex.Split(input, @"[\[email protected]#$%^&*()_+,=-`/?.,<>\|'"";:]");
Dictionary<char, int> countchars = new Dictionary<char, int>();
for (int i = 0; i < words.Length; i++)
{
foreach (var character in words[i])
{
if (countchars.ContainsKey(character))
{
countchars[character]++;
}
else
{
countchars.Add(character, 1);
}
}
}
var sortedLetters = countchars.OrderBy(x => x.Key);
foreach (var letter in sortedLetters)
{
Console.WriteLine("Letter --> {0} - Number --> {1}", letter.Key, letter.Value);
}
}
示例8: Main
static void Main()
{
string text = "It’s your 10 digit Fido number 4%^&[email protected] ( for example [email protected] ).";
string[] words = Regex.Split(text, @"[\[email protected]#$%^&*()_+,=-`/?.,<>\|'"";:]");
Dictionary<char, int> countchars = new Dictionary<char, int>();
for (int i = 0; i < words.Length; i++)
{
foreach (var character in words[i])
{
if (countchars.ContainsKey(character))
{
countchars[character]++;
}
else
{
countchars.Add(character, 1);
}
}
}
var sortedLetters = countchars.OrderBy(x => x.Key);
foreach (var letter in sortedLetters)
{
Console.WriteLine("Letter --> {0} - Number --> {1}", letter.Key, letter.Value);
}
}
示例9: GetTypeKey
private static string GetTypeKey(Dictionary<string, Type> fields)
{
string key = string.Empty;
foreach (var field in fields.OrderBy(v => v.Key).ThenBy(v => v.Value.Name))
key += field.Key + ";" + field.Value.Name + ";";
return key;
}
示例10: Main
public static void Main()
{
var testString = "This is the TEXT. Text, text, text – THIS TEXT! Is this the text?";
var splitted = testString.Split(new char[] { ' ', ',', '.', '?', '!', ';', '–' }, StringSplitOptions.RemoveEmptyEntries)
.Select(w => w.ToLower())
.ToList();
var result = new Dictionary<string, int>();
foreach (var word in splitted)
{
if (result.ContainsKey(word))
{
result[word] += 1;
}
else
{
result[word] = 1;
}
}
var ordered = result.OrderBy(x => x.Value).ToList();
foreach (var item in ordered)
{
Console.WriteLine("{0, 10} --> {1}", item.Key, item.Value);
}
}
示例11: Dump
public static void Dump(Dictionary<string, ObjectMemoryCounters> objects, Dictionary<string, string> totals)
{
try
{
if (objects != null && _objectsLogger != null && _objectsLogger.IsInfoEnabled)
{
foreach (var omc in objects.Values)
{
var collector = new Dictionary<string, string>();
omc.FormatTo(collector);
var data = collector.OrderBy(kvp => kvp.Key);
CheckFirstObjectWrite(data);
_objectsLogger.InfoFormat("{0};{1}", omc.Name, string.Join(";", data.Select(kvp => kvp.Value).ToArray()));
}
}
if (totals != null && _mainLogger != null && _mainLogger.IsInfoEnabled)
{
var data = totals.OrderBy(kvp => kvp.Key);
CheckFirstMainWrite(data);
_mainLogger.Info(string.Join(";", data.Select(kvp => kvp.Value).ToArray()));
}
}
catch
{
// don't care
}
}
示例12: Main
public static void Main()
{
string[] specialSigns = { " ", "!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "-", "_", "+" };
string input = Console.ReadLine();
for (int i = 0; i < specialSigns.Length; i++)
{
input = input.Replace(specialSigns[i], string.Empty);
}
Dictionary<char, int> allLetters = new Dictionary<char, int>();
for (int i = 0; i < input.Length; i++)
{
if (allLetters.ContainsKey(input[i]))
{
allLetters[input[i]]++;
}
else
{
allLetters.Add(input[i], 1);
}
}
var sortedLetters = allLetters.OrderBy(x => x.Key);
foreach (var item in sortedLetters)
{
Console.WriteLine("Letter --> {0} - Number --> {1}", item.Key, item.Value);
}
}
示例13: IsAnagram
public static bool IsAnagram(string a, string b)
{
Dictionary<char, int> dictionary1 = new Dictionary<char, int>();
foreach (var chaR in a)
{
if (dictionary1.ContainsKey(chaR))
{
dictionary1[chaR] += 1;
}
else
{
dictionary1.Add(chaR, 1);
}
}
Dictionary<char, int> dictionary2 = new Dictionary<char, int>();
foreach (var chaR in b)
{
if (dictionary2.ContainsKey(chaR))
{
dictionary2[chaR] += 1;
}
else
{
dictionary2.Add(chaR, 1);
}
}
bool isEqual = dictionary1.OrderBy(r => r.Key).SequenceEqual(dictionary2.OrderBy(r => r.Key));
return isEqual;
}
示例14: Main
static void Main(string[] args)
{
var text = new StreamReader("text.txt").ReadToEnd().ToLower();
// string regex = @"\b\w+\b";
// MatchCollectionollection words = Regex.Matches(text, regex);
var words = text.Split(new char[] { '.', '!', '?', ';', ' ', ':', ',', '-' }, StringSplitOptions.RemoveEmptyEntries);
var dictionary = new Dictionary<string, int>();
foreach (var item in words)
{
if (dictionary.ContainsKey(item))
{
dictionary[item]++;
}
else
{
dictionary.Add(item, 1);
}
}
foreach (var item in dictionary.OrderBy(x => x.Value))
{
Console.WriteLine("{0} -> {1}", item.Key, item.Value);
}
Console.ReadKey(true);
}
示例15: Main
static void Main()
{
string source = Console.ReadLine();
Dictionary<char, int> chars = new Dictionary<char, int>();
for (char i = 'a'; i <= 'z'; i++)
{
chars.Add(i, 0);
}
for (char i = 'A'; i <= 'Z'; i++)
{
chars.Add(i, 0);
}
foreach (char curr in source)
{
if (chars.ContainsKey(curr))
{
chars[curr]++;
}
}
var ordered = chars.OrderBy(x => x.Key); //to sort by the letter
foreach (var item in ordered)
{
if (item.Value != 0)
{
Console.WriteLine("{0} -> {1} ", item.Key, item.Value);
}
}
}