Link to home
Start Free TrialLog in
Avatar of KaranGupta
KaranGupta

asked on

Sorting order in listbox

Hi

I am having following code

using System.Collections.Generic;

namespace Sorting_Issue
{
    class Program
    {
        static void Main(string[] args)
        {
            List<Employee> list = new List<Employee>();
            

            list.Add(new Employee(1, "Abhishek"));
            list.Add(new Employee(2, "Anurag"));
            list.Add(new Employee(3, "karan"));
            list.Add(new Employee(4, "Sachin"));
            list.Add(new Employee(5, "Umesh"));           
                       
        }
    }

    public class Employee
    {
        public Employee(int employeeId, string employeeName)
        {
            EmployeeId = employeeId;
            EmployeeName = employeeName;
        }

        public int EmployeeId { get; set; }
        public string EmployeeName{ get; set; }
    }
}

Open in new window


Now I want to sort the list collection like

karan
           Abhishek
           Umesh
           Sachin
           Anurag

Can we do that
Avatar of Alex_W
Alex_W
Flag of United Kingdom of Great Britain and Northern Ireland image

You can use a .Sort method for the List (of T) object and pass an addressOf argument:

http://msdn.microsoft.com/en-us/library/w56d4y5z.aspx
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Here is code for that:

using System.Collections.Generic;

namespace Sorting_Issue
{
    class Program
    {
        static void Main(string[] args)
        {
            List<Employee> list = new List<Employee>();


            list.Add(new Employee(1, "Abhishek"));
            list.Add(new Employee(2, "Anurag"));
            list.Add(new Employee(3, "karan"));
            list.Add(new Employee(4, "Sachin"));
            list.Add(new Employee(5, "Umesh"));

            list.Sort(new System.Comparison<Employee>(CompareEmployee));

        }

        static int CompareEmployee(Employee e1,Employee e2)
        {
            return (e2.EmployeeId % 2)-(e1.EmployeeId%2);
        }
    }

    public class Employee
    {
        public Employee(int employeeId, string employeeName)
        {
            EmployeeId = employeeId;
            EmployeeName = employeeName;
        }

        public int EmployeeId { get; set; }
        public string EmployeeName { get; set; }
    }
}

Open in new window

Avatar of KaranGupta
KaranGupta

ASKER

Hi All

@Alex_W

Can u please provide me the sample code. It would be helpful.

@Idle_Mind

The link which you have given is not sorting as per my requirement.

@naman_goel

Your code is giving following output -

Karan
Umesh
Abhishek
Anurag
Sachin
"The link which you have given is not sorting as per my requirement."

You haven't really explained your requirements.  You've given the final sort order, but I can't immediately see how they were sorted.  How are you comparing the values to arrive at that state?...
I am not able to understand how you are coming to that output.
Hi

@Idle_Mind

Just want to know - can we define our own sort order. We have a sort method from which we can sort something either in ascending or descending order.

Consider a scenario in which I have a 200 different records. Each record is having its own status like New, In Progress, Closed, Under observation. On page load I want to sort the records according to the status which I want to define whether it can be InProgress, Closed, New and Under Observation or Closed, InProgress, Under Observation and New. I hope I am clear now. Please let me know if you have any queries.

@naman_goel

I have executed the program which you have given and I have got that output
Yes, you can define your own sort order.  As demonstrated in the MSDN example I linked to, you simply implement IComparable() and provide a CompareTo() method in your class and return -1, 0, or 1 to indicate how the two things being compared relate to each other.  Within the CompareTo() function you can use whatever field(s) you need to use to make your decision.
No, I Know you had executed my code for that output but I am just wondering how you are coming to that sort order whatever you are showing in your question.


karan
           Abhishek
           Umesh
           Sachin
           Anurag
Example code for sorting List (Of T):

 Dim dinosaurs As New List(Of String)
        dinosaurs.Add("Pachycephalosaurus")
        dinosaurs.Add("Amargasaurus")
        dinosaurs.Add("")
        dinosaurs.Add(Nothing)
        dinosaurs.Add("Mamenchisaurus")
        dinosaurs.Add("Deinonychus")
        Display(dinosaurs)

        Console.WriteLine(vbLf & "Sort with generic Comparison(Of String) delegate:")
        dinosaurs.Sort(AddressOf CompareDinosByLength)

Open in new window

Hi Alex_W

What is CompareDinosByLength
Hi naman_goel

I have written the scenario for which I need this sorting. Please check the post above your comment.
This is just an example List (Of String) object to show that items are added to the string list and then the .Sort method is called.