[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Sorting order in listbox

Posted on 2012-09-14
13
Medium Priority
?
375 Views
Last Modified: 2012-10-03
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
0
Comment
Question by:KaranGupta
  • 4
  • 3
  • 3
  • +1
13 Comments
 
LVL 8

Expert Comment

by:Alex_W
ID: 38398574
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
0
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 2000 total points
ID: 38398676
You can control how your items are sorted by implementing IComparable() in your Employee() class:
http://msdn.microsoft.com/en-us/library/4d7sx9hd.aspx

*I don't understand your sorting order though!  How did you arrive at that order?...
0
 
LVL 13

Expert Comment

by:Naman Goel
ID: 38399267
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

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:KaranGupta
ID: 38404944
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
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 38405173
"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?...
0
 
LVL 13

Expert Comment

by:Naman Goel
ID: 38405402
I am not able to understand how you are coming to that output.
0
 

Author Comment

by:KaranGupta
ID: 38408197
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
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 38408233
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.
0
 
LVL 13

Expert Comment

by:Naman Goel
ID: 38408273
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
0
 
LVL 8

Expert Comment

by:Alex_W
ID: 38408633
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

0
 

Author Comment

by:KaranGupta
ID: 38409210
Hi Alex_W

What is CompareDinosByLength
0
 

Author Comment

by:KaranGupta
ID: 38409212
Hi naman_goel

I have written the scenario for which I need this sorting. Please check the post above your comment.
0
 
LVL 8

Expert Comment

by:Alex_W
ID: 38409984
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.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Simulator games are perfect for generating sample realistic data streams, especially for learning data analysis. It is even useful for demoing offerings such as Azure stream analytics, PowerBI etc.
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses
Course of the Month18 days, 16 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question