1

I have a class

public class Test
{
    public string Value { get; set; }
}

and then

List<Test> values = new List<Test>()  //contains 10 items

Some Items in the list their Value property can start with the character >

Example:

 Name
 Something 
 Example
 > Another one
 Demo
 Student
 > Home

How can I sort my list of objects so that the first items in the list are sorted alphabetically and the ones that start with > are sorted alphabetically as well but are at the end of the list?

This is what I did so far:

values.Where(x => !x.Value.StartsWith(">")).OrderBy(x => x.Value);
eran otzap
  • 12,293
  • 20
  • 84
  • 139
user2818430
  • 5,853
  • 21
  • 82
  • 148
  • 1
    I have edited your title. Please see, "[Should questions include “tags” in their titles?](http://meta.stackexchange.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders May 11 '15 at 05:51
  • And why are you trieng to do everything in 1 line? You could first filter all Objects with a Value that stats with ">" out and then sort. So you could debug it better.. – Jens May 11 '15 at 05:54
  • 1
    Please [see this answer](http://stackoverflow.com/a/3309293/3538012) at the duplicate question. You should sort everything by whether it has `>` or not, and then use `ThenBy()` (as shown in that answer) to sort alphabetically. If you make an attempt to implement that but have trouble, please ask a new question, and include in that question [a good, _minimal_, _complete_ code example](http://stackoverflow.com/help/mcve) that clearly illustrates whatever difficulty you're having. – Peter Duniho May 11 '15 at 05:55

1 Answers1

2

you would need to calculate a key to sort by

List<Test> ordered = values.OrderBy(v => 
         v.Value.StartsWith(">") ? "ZZ" + v.Value : v.Value
).ToList();

"ZZ" is an arbitrary key that will sort the values starting with ">" after all English terms. (Being the last letter in the alphabet and no English words starting with two Zs). SInce ">" has a lower byte value than any letter one Z is not enough, since any valid english term would sort after anything staring with Z>

Rune FS
  • 21,497
  • 7
  • 62
  • 96