c# - What is the best way to sort a List of objects with a start value? -
what best way sort list of object start value? list has items:
obj obj b obj c obj d a.index = 1 b.index = 3 c.index = 2 d.index = 4 start value = 3;
sorted list must {b,d,a,c}
thanks help.
use orderby
, thenby
combination. work using system.linq
@ top of file.
var source = new list<item>() { new item { index = 1, value = 'a' }, new item { index = 3, value = 'b' }, new item { index = 2, value = 'c' }, new item { index = 4, value = 'd' }, }; int startvalue = 3; var sortedlist = source.orderby(i => i.index < startvalue) .thenby(i => i.index) .tolist(); foreach (var item in sortedlist) console.writeline(string.format("{0} - {1}", item.value, item.index));
item
class definition:
class item { public char value { get; set; } public int index { get; set; } }
returns desired output:
b - 3 d - 4 - 1 c - 2
Comments
Post a Comment