Hi I currently have a list of strings with values:
*Item1, *Item2, Test2, Test4, *Item3, Test1, *Item4, *Item5
How do I re-arrange them and have all the values with asterisk to be in the first part of the list such as this while maintaining the order of the non-asterisk items:
*Item1, *Item2, *Item3, *Item4, *Item5, Test2, Test4, Test1
Simple OrderByDescending
will do the thing
var str = "*Item1, *Item2, Test2, Test4, *Item3, Test1, *Item4, *Item5";
var arr = str.Split(new char[] { ' ', ',' }, StringSplitOptions.RemoveEmptyEntries);
var ordered = arr.OrderByDescending(x => x.StartsWith("*")).ToArray();
您使用哪个标准进行排序?没有 list.Sort();为你解决吗?
string[] items={"*Item1", "*Item2", "Test2", "Test4", "*Item3", "Test1", "*Item4", "*Item5"}; var result=items.OrderBy(x=>x);
这回答了你的问题了吗?LINQ Orderby 降序查询
目前尚不清楚您是否要订购星号部分,只需将它们放在前面即可。在您的示例中,所有 asterix 都已订购,使得这种特殊情况不清楚。
Thanks for this! Marking as the answer :)
This simply put the asterisk first. "while maintaining the order of the non-asterisk items"., perhaps imply it was only for the non-asterisk.