0

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
ohhzumm
  • 98
  • 1
  • 12
  • which criteria are you using for sort? doesn't `list.Sort();` solve it for you? – Hoshani Oct 14 '20 at 07:11
  • `string[] items = {"*Item1", "*Item2", "Test2", "Test4", "*Item3", "Test1", "*Item4", "*Item5"}; var result = items.OrderBy(x=>x);` – Maciej Los Oct 14 '20 at 07:12
  • 1
    Does this answer your question? [LINQ Orderby Descending Query](https://stackoverflow.com/questions/5344805/linq-orderby-descending-query) – Hoshani Oct 14 '20 at 07:13
  • 2
    It's unclear if you want to order the asterisk part on simply put them first. In your example all asterix are already ordered making this special case unclear. – Drag and Drop Oct 14 '20 at 07:29

1 Answers1

0

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();
Yehor Androsov
  • 4,885
  • 2
  • 23
  • 40