Mark
Mark

Reputation: 7818

Sorting a list of objects with OrderByDescending

I have an object (KS), which holds ID and Title (which has a number as part of the Title).

All I'm trying to do is sort it into descending order. The object has:

ID    Title
1     1 Outlook VPN 
2     2 Outlook Access
3     4 Access VBA
4     3 Excel Automation

So when order by Title, it should read:

ID    Title
3     4 Access VBA
4     3 Excel Automation
2     2 Outlook Access
1     1 Outlook VPN 

The code I'm using to sort it is:

IEnumerable<KS> query = results.OrderByDescending(x => x.Title);

However, query still has the objects in the original order!

Is there something to do with having numbers at the start of Title that I'm missing?

EDIT

I've added the code from the controller for clarity:

    [HttpPost]
    // [ValidateAntiForgeryToken]
    // id is a string of words eg: "outlook access vpn"
    // I split the words and want to check the Title to see how many words appear
    // Then sort by the most words found
    public JsonResult Lookup(string id)
    {
        List<string> listOfSearch = id.Split(' ').ToList();
        var results = db.KS.Where(x => listOfSearch.Any(item => x.Title.Contains(item)));

        // search each result, and count how many of the search words in id are found
        // then add the count to the start of Title
            foreach (KS result in results)
            {
                result.KSId = 0;
                foreach (string li in listOfSearch)
                {
                    if (result.Title.ToLower().Contains(li.ToLower()))
                    {
                        result.KSId += 1;
                    }
                }
                result.Title = result.KSId.ToString() + " " + result.Title;
            }
       // sort the results based on the Title - which has number of words at the start
       IEnumerable<KS> query = results.OrderByDescending(x => x.Title).ToList();
       return Json(query, JsonRequestBehavior.AllowGet);
    }

Here is a screenshot after query has been populated showing Titles in the order: 1, 2, 1, 1:

VSSS

Model for the object if it helps is:

 public class KS
{
    public int KSId { get; set; }
    public string KSSol { get; set; }
    public string Title { get; set; }
    public string Fix { get; set; }
}

Upvotes: 1

Views: 5755

Answers (5)

Xiokraze
Xiokraze

Reputation: 381

Old question, but maybe this will help someone using C#. I used the following expressions to sort a list of objects based on their quantity parameter in ascending or descending order. Can modify it to compare text as the original question was concerned with.

Ascending Order:

locationMaterials.Sort((x, y) => x.Quantity.CompareTo(y.Quantity));

Descending Order:

locationMaterials.Sort((x, y) => y.Quantity.CompareTo(x.Quantity));

Upvotes: 3

Jens Kloster
Jens Kloster

Reputation: 11287

You are missing .ToList()

IEnumerable<KS> query = results.OrderByDescending(x => x.Title).ToList();

results.OrderByDescending(x => x.Title) is a query, and it has no data. ToList() forces the query to be executed.

[EDIT]

My answer assumes that your results has acually not been materialized, and that that is the source of your problem.

Upvotes: -1

Derek
Derek

Reputation: 8628

I simple did this and it worked for me :-

var sortedOrder = Query.OrderBy(b => b.Title.Substring(b.Title.IndexOf(" ")));

All I have done is SubString the Title at the index of of the blank space when ordering the objects in the sequence, that way, the OrderBy is looking at the first character in the title rather than the number at the beginning.

Upvotes: 0

YoryeNathan
YoryeNathan

Reputation: 14522

You can always just ignore the strange behavior and go the safe way:

List<KS> query = results.ToList();
query.Sort((a, b) => a.Whatever.CompareTo(b.Whatever));

return Json(query, blah);

Upvotes: 0

Jeppe Stig Nielsen
Jeppe Stig Nielsen

Reputation: 61982

As I said in a comment, put a .ToList() where you declare your results variable. That is:

var results = db.KS.Where(x => listOfSearch.Any(item => x.Title.Contains(item)))
    .ToList();

If you don't do that, the foreach loop will modify objects that might not be the same as the objects you sort later, because the database query is run again each time you enumerate your IQueryable<>.

Upvotes: 2

Related Questions