sorting a hashtable or a dictionary

Posted on 2007-08-06
Last Modified: 2013-11-05

I have a dataset where each row represents the results for a team in a rugby  league table. It has columsn for team name, total points, goal difference, goals for and goals against. I was wondering what would be the easiest way to sort this dataset so that the teams were ordered in the correct order for the table (e.g highest points at the top, if 2 teams have the same points then the one with highest goal difference is second)

This might be a basic question. I'm asking because I am interested in knowing an efficient way of doing this. I once asked something like this and got accused of doing homework. This isn't homework. I'm 32 and its for the rugby world cup :)

many thanks in advance
Question by:andieje
    LVL 46

    Expert Comment

    by:Wayne Taylor (webtubbs)
    Hello andieje,

    Do you wish to sort a dataset, or a hashtable/dictionary?

    To sort a dataset's table, you'd use a DataView....

            Dim dv As New DataView(ds.Tables(0))
            dv.Sort = "[Total Points]"


    LVL 38

    Accepted Solution

    The easiest way to sort a datatable is to use "ORDER BY [Fieldname]" in the SQL that generates it.  If that isn't an option, you can set up a DataView which represents a view of the DataTable that you can sort, filter, etc.


    Dim dv as New DataView(MyDataTable)
    dv.Sort("Fieldname Asc")  'Ascending sort.

    DataView sort:

    Then you can use the dataview as a data source.  If you search the MSDN for dataview, you'll find some good stuff there.
    LVL 96

    Expert Comment

    by:Bob Learned
    You don't even need to create a new DataView:

    DataSet1.Tables(0).DefaultView.Sort = "Name"
    Me.dg.DataSource = DataSet1.Tables(0).DefaultView


    Author Comment

    Hi Paul

    You're right - sorting is a database thing!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Article by: jpaulino
    XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String ( Literal, only instead of starting and ending with w…
    If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    779 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

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now