score:14

Accepted answer

Assuming you want to prioritise strings based on the position of *, you could do

bunchOfStrings.OrderByDescending(x => x.IndexOf("*"))

Use OrderByDescending because for the strings that don't contain * they will return -1.


Actually, looking further into this it's not going to work straight out the box with IndexOf. OrderByDescending will work by going for the highest ranked index, which in your case is going to be this should be *somewhere close to the top rather than this *SHOULD be at the top because the * has a higher index in that string.

So to get it to work you just need to manipulate the rankings a little and use OrderBy instead

bunchOfStrings.OrderBy(x => {
    var index = x.IndexOf("*");
    return index < 0 ? 9999 : index;
});

Note - 9999 is just some aribtrary value that we can assume IndexOf will never exceed

See live example

score:2

If Contains is what you want to use..

Contains returns boolean - so you're ordering by true or false. Since true is 1 and 0 is false - you're ordering them backwards from what you want. So you want OrderByDescending:

bunchOfStrings.OrderByDescending(x => x.Contains("*"))

Which sorts 1 -> 0.

Click here for live example on IDEOne.


Related Articles