Advertisement
NPSF3000

Sorted_List_Search

Jan 16th, 2012
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.06 KB | None | 0 0
  1. //  Thread http://forum.unity3d.com/threads/119735-Finding-a-similar-key-in-a-Dictionary]
  2. //  Copyright NPSF3000 2012.  ALL RIGHTS RESERVED.  NO WARRANTY GIVEN.  
  3. //  ATTRIBUTION REQUIRED FOR ALL NON-PERSONAL APPLICATIONS
  4.  
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Text;
  9.  
  10. namespace Sorted_List_Search
  11. {
  12.     class Program
  13.     {
  14.         static void Main(string[] args)
  15.         {
  16.             var dates = new SortedList<int, string>
  17.             {
  18.                 {1,"r"},
  19.                 {5,"g"},
  20.                 {9,"b"},
  21.                 {10,"x"},
  22.                 {15,"y"},
  23.             };
  24.  
  25.             Console.WriteLine("Enter a positive integer and press enter.");
  26.             Console.WriteLine();
  27.  
  28.             while (true)
  29.             {
  30.                 var input = Console.ReadLine();
  31.                 int key;
  32.                 if (int.TryParse(input, out key))
  33.                     Console.WriteLine(dates.Select(x => x).Where(x => x.Key <= key).OrderByDescending(x => x.Key).First());
  34.             }
  35.         }
  36.     }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement