Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- A
- B
- A
- C
- B
- C
- D
- E
- firstPref = 'A';
- secondPref = 'B';
- thirdPref = 'C';
- A
- A
- B
- B
- C
- C
- D
- E
- List<String> data = new List<String> { "A","B","A","C","B","C","D","E" };
- List<String> preferences = new List<String> { "A","B","C" };
- IEnumerable<String> orderedData = data.OrderBy(
- item => preferences.IndexOf(item));
- IEnumerable<String> orderedData = data.OrderByDescending(
- item => Enumerable.Reverse(preferences).ToList().IndexOf(item));
- IEnumerable<String> orderedData = data.OrderBy(
- item => preferences.Concat(data).ToList().IndexOf(item));
- string[] svals = new string[] {"A", "B", "A", "C", "B", "C", "D", "E"};
- List<string> list = svals.OrderBy(a => a, new CustomComparer()).ToList();
- private class CustomComparer : IComparer<string>
- {
- private string firstPref = "A";
- private string secondPref = "B";
- private string thirdPref = "C";
- public int Compare(string x, string y)
- {
- // first pref
- if (y == firstPref && x == firstPref)
- return 0;
- else if (x == firstPref && y != firstPref)
- return -1;
- else if (y == firstPref && x != firstPref)
- return 1;
- // second pref
- else if (y == secondPref && x == secondPref)
- return 0;
- else if (x == secondPref && y != secondPref)
- return -1;
- else if (y == secondPref && x != secondPref)
- return 1;
- // third pref
- else if (y == thirdPref && x == thirdPref)
- return 0;
- else if (x == thirdPref && y != thirdPref)
- return -1;
- else
- return string.Compare(x, y);
- }
- }
- List<string> data = new List<string> {
- "E", "B", "D", "A", "C", "B", "A", "C"
- };
- var preferences = new Dictionary<string, string> {
- { "A", " 01" },
- { "B", " 02" },
- { "C", " 03" }
- };
- string key;
- IEnumerable<String> orderedData = data.OrderBy(
- item => preferences.TryGetValue(item, out key) ? key : item
- );
Add Comment
Please, Sign In to add comment