Note that there are some explanatory texts on larger screens.

plurals
  1. POSorting a list of objects with OrderByDescending
    primarykey
    data
    text
    <p>I have an <code>object (KS)</code>, which holds <code>ID and Title</code> (which has a number as part of the Title).</p> <p>All I'm trying to do is sort it into descending order. The object has:</p> <pre><code>ID Title 1 1 Outlook VPN 2 2 Outlook Access 3 4 Access VBA 4 3 Excel Automation </code></pre> <p>So when order by Title, it should read:</p> <pre><code>ID Title 3 4 Access VBA 4 3 Excel Automation 2 2 Outlook Access 1 1 Outlook VPN </code></pre> <p>The code I'm using to sort it is:</p> <pre><code>IEnumerable&lt;KS&gt; query = results.OrderByDescending(x =&gt; x.Title); </code></pre> <p>However, <strong>query</strong> still has the objects in the original order!</p> <p>Is there something to do with having numbers at the start of <strong>Title</strong> that I'm missing?</p> <p><strong>EDIT</strong></p> <p>I've added the code from the controller for clarity:</p> <pre><code> [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&lt;string&gt; listOfSearch = id.Split(' ').ToList(); var results = db.KS.Where(x =&gt; listOfSearch.Any(item =&gt; 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&lt;KS&gt; query = results.OrderByDescending(x =&gt; x.Title).ToList(); return Json(query, JsonRequestBehavior.AllowGet); } </code></pre> <p>Here is a screenshot <strong>after</strong> query has been populated showing Titles in the order: 1, 2, 1, 1:</p> <p><img src="https://i.stack.imgur.com/hqTp6.png" alt="VSSS"></p> <p>Model for the object if it helps is:</p> <pre><code> public class KS { public int KSId { get; set; } public string KSSol { get; set; } public string Title { get; set; } public string Fix { get; set; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload