18741

Use LINQ for arbitrary sorting

Question:

Let's say we have an entity that has attributes att1 and att2, where att1 can have values a,b,c and att2 can have values 1,2,3. Is it possible to use LINQ so that we can sort items in the collection by applying arbitrary sorting rule without implementing IComparable. I am facing an issue were business requires that on some screens items in the collection be sorted one way and in other screens some other way. For example rule can state that items need to be sorted so that "b" is listed first, then "a", then "c" and within each group, "3" is first, then "1" then "2".

Answer1:

Sure. You can use OrderBy with a predicate that returns more or less whatever kind of arbitrary "sort order" you please. For example:

objectsWithAttributes.OrderBy(x => { // implement your "rules" here -- anything goes as long as you return // something that implements IComparable in the end. this code will sort // the enumerable in the order 'a', 'c', 'b' if (x.Attribute== 'a') return 0; else if (x.Attribute== 'c') return 1; else if (x.Attribute== 'b') return 2; }).ThenBy(x => { // implement another rule here? });

Recommend

  • illegal attempt to associate a collection with two open sessions fluent nhibernate
  • How to sort array without using sort method in C# [closed]
  • How to store tree structure in sql?
  • jquery/javascript convert a plain text message into a text input field
  • object oriented Casting error [duplicate]
  • Group objects by equality
  • Python functions: Pass global variables if only accessing them?
  • angular 2 group validation
  • Dropping support for JRE 1.3
  • Conditional Formatting in VBA, based on functions
  • Python: remove double quotes from JSON dumps
  • Merging Users in Kinvey
  • Using recursion to search all combinations of elements in an array of integers
  • OneToOne bidirectional mapping foreign key auto fill
  • allow scroll of div set behind another div
  • Problem glTexGen in Open GL ES 2.0
  • VBA: Extract Top 'x' Entries from each category
  • PXAction seemingly does nothing
  • Use of qualified name in function parameter
  • SSRS 2008 - Sorting within a group
  • Peer to peer/adaptive payments with paypal [closed]
  • Cassandra: What is a subcolumn
  • PHP: Very simple Encode/Decode string
  • Fixed Background Works in Chrome but Not Firefox?
  • Complex trait requirements on struct
  • Simple Factory with reflection C#
  • Build Matrix of Comparisons in SQl Server
  • View Paypal shopping cart contents on my site
  • composer dependency stating in doesn't have php-xsl
  • How to select table rows/complete table?
  • Calling C function from lua
  • Zeromq with python hangs if connecting to invalid socket
  • Wait for .each() .getJSON request to finish before executing a callback
  • Is there a way to choose which files are displayed to the user via the standard OPENFILE dialogs?
  • F#: In which memory area is the continuation stored: stack or heap?
  • A class implementing two different IObservables?
  • python: forcing relative imports to search from script file
  • Apache RewriteRule redirection with url encoded
  • Firefox Extension - Monitor refresh and change of tab
  • Sort List of Strings By Version
  • $wpdb not working in file of WordPress plugin
  • Insert into database using onclick function
  • Display issues when we change from one jquery mobile page to another in firefox
  • Different response to non-authenticated users and AJAX calls
  • Arrow is showed instead of the material design version hamburger icon. Why doesn't syncState in
  • Data Validation Drop Down Box Arrow Disappearing
  • CSS Applying specific rule for a specific monitor resolution with only CSS is posible?
  • Does armcc optimizes non-volatile variables with -O0?