Click here to Skip to main content
15,883,558 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I am sorting a list based on priority value and it is integer
C#
lstRecData = lstRecData.OrderBy(x => x.strRecPriority).Reverse().ToList();

But the ouput is not changed it is showing 4,3,2 instead 2,3,4
Posted
Updated 29-Oct-14 19:19pm
v2
Comments
BillWoodruff 30-Oct-14 1:41am    
Unless you tell us how you define 'priority, and how you calculate it, we'll never be able to help you. What is 'strRecPrioriry: where/how is it defined.

Hi..

Make little bit change in your code line ..

change it to

C#
lstRecData = lstRecData.OrderBy(x => x.strRecPriority).ToList();


Here is the sample code

C#
class Program
    {
        static void Main(string[] args)
        {
            List<priority> lstRecData = new List<priority>();

            priority obj1 = new priority();
            priority obj2 = new priority(); 
            priority obj3 = new priority();
            obj1.strRecPriority = "2";
            obj2.strRecPriority = "1";
            obj3.strRecPriority = "3";

            lstRecData.Add(obj1);
            lstRecData.Add(obj2);
            lstRecData.Add(obj3);

            lstRecData = lstRecData.OrderBy(x => x.strRecPriority).ToList();
            foreach (priority obj in lstRecData)
            {
                Console.WriteLine(obj.strRecPriority);  
            }

        }
    }
    public class priority
    {
        public string strRecPriority { get; set;}
    }
</priority></priority>
 
Share this answer
 
v2
Hi
Just avoid Reverse().it will be fine.
C#
List<order> lstRecData = lstRecData.OrderBy(x => x.strRecPriority).ToList();</order>
 
Share this answer
 
Try this sample code:
C#
using System;
using System.Collections.Generic;
using System.Linq;


public class SomeClass
{
    public string name { get; set; }
    public int strRecPriority { get; set; }

    public SomeClass(string name, int strRecPriority)
    {
        this.name = name;
        this.strRecPriority = strRecPriority;
    }
}

public class Program
{
    public static void Main()
    {
        List<SomeClass> list = new List<SomeClass>();
        list.Add(new SomeClass("Apple", 100));
        list.Add(new SomeClass("Banana", 50));
        list.Add(new SomeClass("Orange", 97));
        list.Add(new SomeClass("Pear", 110));
        list.Add(new SomeClass("Strwberry", 88));

        var result1 = list.OrderByDescending(a => a.strRecPriority).ToList<SomeClass>();

        foreach (SomeClass o in result1)
        {
            Console.WriteLine(o.name + " " + o.strRecPriority);
        }
    }
}
 
Share this answer
 
C#
var result1 = list.OrderBy(a => a.strRecPriority).ToList<someclass>();
</someclass>


Try this code. It will return
50, 88, 77, 100, 110
 
Share this answer
 
if you have string value, convert to int and then apply order by
C#
lstRecData = lstRecData.OrderByDescending(x => Convert.ToInt32(x.strRecPriority)).ToList();
 
Share this answer
 
Comments
chandra sekhar 30-Oct-14 1:32am    
OrderBy or OrderByDescending?? Shall i use .Reverse()??
DamithSL 30-Oct-14 1:34am    
what is the result you get? you can use OrderByDescending.. otherwise you need to do two operations OrderBy and Reverse
Philippe Mori 30-Oct-14 21:38pm    
It does not make much sense to use Reverse when OrderByDescending can be used. By the way, as mentionned it Solution 7, you probably don't need to reverse the order.
Hi,


lstRecData.OrderBy(x => x.strRecPriority).Reverse().ToList();


You have put order by first so it will sort it in a ascending manner then you put reverse its to descending manner.


Use this :

lstRecData.OrderBy(x => Convert.ToInt(x.strRecPriority)).ToList();
 
Share this answer
 
v3
Comments
chandra sekhar 30-Oct-14 1:07am    
Still it is the same not sorted.
Suvabrata Roy 30-Oct-14 1:34am    
Convert.ToInt(strRecPriority) is required
Philippe Mori 30-Oct-14 21:40pm    
Why not sort directly in descending order if this is what it is desired. But as mentionned in solution 7 and given the actual output of the question, reverse should be removed.
Suvabrata Roy 31-Oct-14 4:47am    
what is the difference between solution 7 and mine...?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900