Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List sorting C# using 2 fields

I have a list of my custom objects. The object contains 1 string and 2 decimals. I would like to sort the list based on the 2nd decimal field descending then the first decimal field.

For eg:

object 1 -> "a", 100, 10
object 2 -> "b", 300, 0
object 3 -> "c", 200, 200
object 4 -> "b", 400, 0

would be sorted as object 3, object 1, object 4, object 2

I apologize if this has already been answered - please point me to that post as I could not find it

like image 591
Vijay V Avatar asked Aug 23 '13 22:08

Vijay V


People also ask

Which sort is best for linked list?

Merge sort is often preferred for sorting a linked list. The slow random-access performance of a linked list makes some other algorithms (such as quicksort) perform poorly, and others (such as heapsort) completely impossible.

How do you sort data in a linked list?

Below is a simple insertion sort algorithm for a linked list. 1) Create an empty sorted (or result) list 2) Traverse the given list, do following for every node. ......a) Insert current node in sorted way in sorted or result list. 3) Change head of given linked list to head of sorted (or result) list.

What is sorting in C with example?

Selection sort first finds the smallest element in the unsorted array and swaps it with the first element. Then it finds the second smallest element in the unsorted array and swaps it with the second element, and the algorithm keeps doing this until the entire array is sorted.

Which is the best sorting algorithm in C?

Quicksort. Quicksort is one of the most efficient sorting algorithms, and this makes of it one of the most used as well. The first thing to do is to select a pivot number, this number will separate the data, on its left are the numbers smaller than it and the greater numbers on the right.


1 Answers

list.OrderByDescending(o => o.Field2)
    .ThenByDescending(o => o.Field1);
like image 51
D Stanley Avatar answered Oct 03 '22 07:10

D Stanley