1

I have a list of strings shows below. I want to sort the name based on the Start with value. I try some cases but fails. Example List has data like,

name: "Bible Reading", name: "Scripture Reading", name: "Ready Set", name: "Career Readings", name: "Reading-Berks" 

I pass the queryValue as "read" but I can not be ordering based on the values.

IOrderedEnumerable<string> name = result.Select(x => x.name) .OrderBy(i => i.StartsWith(queryValue)); 

The expected output,

name: "Ready Set", name: "Reading-Berks" name: "Bible Reading", name: "Scripture Reading", name: "Career Readings", 
1
  • 1
    What did you try? How are you ordering right now? Commented Jun 27, 2016 at 6:57

2 Answers 2

3

I think this is a issue about case sensitivity.

replace

.OrderBy(i => i.StartsWith(queryValue)); 

with

.OrderByDescending(i => i.StartsWith(queryValue, StringComparison.InvariantCultureIgnoreCase)); 

because "Ready Set".StartsWith("read") will return false

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. Its working now, FYI use "OrderByDescending: instead of orderby
1

You have to use OrderByDescending and case insensitive comparison for the required result, hence your query will be like the following:

IOrderedEnumerable<string> name = result.Select(x => x.name) .OrderByDescending(i =>i.StartsWith(queryValue,StringComparison.InvariantCultureIgnoreCase)); 

Here is a working example which will distinguish OrderByDescending and OrderBy for this example

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.