How to Sort a Generic List

Sorting a generic list is useful in generating a structured record of whatever items you may have in your list. This application is useful in collating customer records, a vendor directory, or even a membership roster. As long as you have a list of items you need to organize, you can put all your data into your code and let the computer sort everything out. There are hundreds of programming languages you can use to implement the sorting of generic lists. Here are a few:
Visual Basic
Java
C#
Visual C
C++
Jscript
VB.NET
ASP.NET
Most of the commands for sorting generic lists are similar throughout different programming languages. This article provides the steps and sample code for sorting a generic list using C#.

Things You'll Need

  • Programming application
Show More

Instructions

    • 1

      Determine the generic list you want to sort. You can use a club's membership roster, for example.

    • 2

      Break down the elements of your generic list into sortable attributes. For example: First name and last name; Age and sex; etc.

    • 3

      Add your attributes into the generic list code. The following sample code shows how you can put the Age and Name attributes of your membership roster into a list:
      class Member
      {
      private int _ Age;
      private string _ Name;

      public int Age
      {
      get
      {
      return _ Age;
      }
      set
      {
      _Age = value;
      }
      }
      public string Name
      {
      get
      {
      return _Name;
      }
      set
      {
      _Name = value;
      }
      }
      }

    • 4

      Add the Membership generic list (also known as the Business Object) into your program by using this line of code:
      List<Member> cuslist = new List<Member>();

    • 5

      Use the following overload to sort your generic list:
      List.Sort ()

    • 6

      Implement the overload in your program with the following sample code:
      class Member: IComparable < Member >
      {
      //Members
      //1st Overload
      public int CompareTo(Member mem)
      {
      return this.Name.CompareTo(mem.Name);
      }
      }

    • 7

      Enter the membership details (first name and age) into the generic list of your code:
      List < Member > cuslist1 = new List < Member > ();
      Member mem1 = new Member();
      mem1.Name = "Alice";
      mem1.Age = 21;
      memlist1.Add(mem1);

      Member mem2 = new Member();
      mem2.Name = "Gina";
      mem2.Age = 27;
      memlist1.Add(mem2);

      Member mem3 = new Member();
      mem3.Name = "Leticia";
      mem3.Age = 39;
      memlist1.Add(mem3);

      Member mem4 = new Member();
      mem4.Name = "Sandra";
      mem4.Age = 23;
      memlist1.Add(mem4);

      Member mem5 = new Member();
      mem5.Name = "Donna";
      mem5.Age = 29;
      memlist1.Add(mem5);

      memlist1.Sort();
      foreach (Member mem in memlist1)
      {
      Console.WriteLine(mem.Name + " " + mem.Age);
      }

    • 8

      Compile your program and run the sorted generic list to get the following result:
      Alice 21
      Donna 29
      Gina 27
      Leticia 39
      Sandra 23

    • 9

      Implement additional overloads, as necessary.

Tips & Warnings

  • Download free sample code when you can; there are many resources over the Internet. Using free code saves you time and money.

  • Work with the programming language you are most comfortable with. Many programming software are easy to learn and use, such as Visual Basic and Java.

Related Searches:

References

Resources

Comments

You May Also Like

  • How to Sort a Linked List in Java

    A linked list is one of the primary types of data structures in the programming world. It's an arrangement of nodes which...

  • Advantages of Visual Basic

    Advantages of Visual Basic. If you are looking for a programming language to help you create Window's applications like Word and Excel,...

  • Visual Basic ListView Tutorial

    The ListView control in Visual Basic became popular with Windows Explorer. This control works in conjunction with the TreeView control and is...

  • How to Remove a Generic Dropper

    The generic dropper is a Trojan horse virus program. It attaches to seemingly harmless programs, such as updates or scripts required to...

  • Different Data Types in Visual Basic

    Different Data Types in Visual Basic. Visual Basic has 17 basic data types that are used to construct all the data stored...

  • How to Sort Lists Using Word 2002

    Choose the item from the drop down list that will function as your sort criteria. For example, choose "Paragraph" from the list...

  • How Do I Sort My Favorites List?

    Humans love to collect things, and a web browser's "bookmarks" section is where we collect our favorite webpages. But like any collection,...

  • What Is Adware Generic?

    Adware Generic is a type of program known as malware, which is any malicious software that intends to harm your computer or...

  • How to Sort in Visual Basic

    Sorting data for your users helps with the usability of your application. Visual Basic components come with a "Sort()" function that provides...

Related Ads

Featured