Guest User

Untitled

a guest
Jun 23rd, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.03 KB | None | 0 0
  1. A
  2. B
  3. A
  4. C
  5. B
  6. C
  7. D
  8. E
  9.  
  10. firstPref = 'A';
  11. secondPref = 'B';
  12. thirdPref = 'C';
  13.  
  14. A
  15. A
  16. B
  17. B
  18. C
  19. C
  20. D
  21. E
  22.  
  23. List<String> data = new List<String> { "A","B","A","C","B","C","D","E" };
  24. List<String> preferences = new List<String> { "A","B","C" };
  25.  
  26. IEnumerable<String> orderedData = data.OrderBy(
  27. item => preferences.IndexOf(item));
  28.  
  29. IEnumerable<String> orderedData = data.OrderByDescending(
  30. item => Enumerable.Reverse(preferences).ToList().IndexOf(item));
  31.  
  32. IEnumerable<String> orderedData = data.OrderBy(
  33. item => preferences.Concat(data).ToList().IndexOf(item));
  34.  
  35. string[] svals = new string[] {"A", "B", "A", "C", "B", "C", "D", "E"};
  36. List<string> list = svals.OrderBy(a => a, new CustomComparer()).ToList();
  37.  
  38. private class CustomComparer : IComparer<string>
  39. {
  40. private string firstPref = "A";
  41. private string secondPref = "B";
  42. private string thirdPref = "C";
  43. public int Compare(string x, string y)
  44. {
  45. // first pref
  46. if (y == firstPref && x == firstPref)
  47. return 0;
  48. else if (x == firstPref && y != firstPref)
  49. return -1;
  50. else if (y == firstPref && x != firstPref)
  51. return 1;
  52. // second pref
  53. else if (y == secondPref && x == secondPref)
  54. return 0;
  55. else if (x == secondPref && y != secondPref)
  56. return -1;
  57. else if (y == secondPref && x != secondPref)
  58. return 1;
  59. // third pref
  60. else if (y == thirdPref && x == thirdPref)
  61. return 0;
  62. else if (x == thirdPref && y != thirdPref)
  63. return -1;
  64. else
  65. return string.Compare(x, y);
  66. }
  67. }
  68.  
  69. List<string> data = new List<string> {
  70. "E", "B", "D", "A", "C", "B", "A", "C"
  71. };
  72. var preferences = new Dictionary<string, string> {
  73. { "A", " 01" },
  74. { "B", " 02" },
  75. { "C", " 03" }
  76. };
  77.  
  78. string key;
  79. IEnumerable<String> orderedData = data.OrderBy(
  80. item => preferences.TryGetValue(item, out key) ? key : item
  81. );
Add Comment
Please, Sign In to add comment