Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7990729
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T13:06:01+00:00 2026-06-04T13:06:01+00:00

Im building a datatable with 2 columns with listview and i need to order

  • 0

Im building a datatable with 2 columns with listview and i need to order the datatable by the second column.

It look something like this:

Name – Points

——————

John      –  10
Peter     –  14
Marcus  –  9

So how do I order it by points?



SOLVED!!

        private class PointsComparer : IComparer
        {
            private const int pointsColumnIndex = 1;

            public int Compare(object x, object y)
            {
                ListViewItem listX = (ListViewItem)x;
                ListViewItem listY = (ListViewItem)y;

                // Convert column text to numbers before comparing.
                // If the conversion fails, just use the value 0.
                decimal listXVal, listYVal;
                try
                {
                    listXVal = Decimal.Parse(listX.SubItems[pointsColumnIndex].Text);
                }
                catch
                {
                    listXVal = 0;
                }

                try
                {
                    listYVal = Decimal.Parse(listY.SubItems[pointsColumnIndex].Text);
                }
                catch
                {
                    listYVal = 0;
                }

                return (-Decimal.Compare(listXVal, listYVal));
            }
        }

This worked as a charm for me.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T13:06:03+00:00Added an answer on June 4, 2026 at 1:06 pm

    Use ListViewItemSorter property to set custom IComparer for your items. Then simply call Sort() method:

    // adding data
    listView1.Items.Add(new ListViewItem(new string[] { "John", "10" }));
    listView1.Items.Add(new ListViewItem(new string[] { "Peter", "14" }));
    listView1.Items.Add(new ListViewItem(new string[] { "Markus", "9" }));
    // setting comparer and sorting
    listView1.ListViewItemSorter = new PointsComparer();
    listView1.Sort();
    

    Here is sample of comparer which you can use to compare points:

    private class PointsComparer : IComparer
    {
        private const int pointsColumnIndex = 1;
    
        public int Compare(object x, object y)
        {
            int pointsX = Int32.Parse(((ListViewItem)x).SubItems[pointsColumnIndex].Text);
            int pointsY = Int32.Parse(((ListViewItem)y).SubItems[pointsColumnIndex].Text);
            return pointsX.CompareTo(pointsY);
        }
    }
    

    Or, as I posted previously, you can use comparer by column text from msdn sample.

    UPDATE: You can pass desired sort order to instance of your comparer:

    private class PointsComparer : IComparer
    {
        private const int pointsColumnIndex = 1;
        private SortOrder _sortOrder;            
    
        public PointsComparer(SortOrder sortOrder)
        {
            _sortOrder = sortOrder;
        }
    
        public int Compare(object x, object y)
        {
            int pointsX = Int32.Parse(((ListViewItem)x).SubItems[pointsColumnIndex].Text);
            int pointsY = Int32.Parse(((ListViewItem)y).SubItems[pointsColumnIndex].Text);
            int comparisonResult = pointsX.CompareTo(pointsY);
    
            switch (_sortOrder)
            {
                case SortOrder.Ascending:
                    return comparisonResult;
                case SortOrder.Descending:
                    return (-1) * comparisonResult;
                default:
                    return 0;
            }
        }
    }
    

    Then use it like this:

    listView1.ListViewItemSorter = new PointsComparer(SortOrder.Descending);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Building a website. When I order my tags like this, LightCycle works but Lightbox
I am building a CMS for editing page content. I would like to make
I want to try out building a simple grid that has a delete column,
I am building a ASP.NET page which is an Sales Order Processing Form I
I have a scenario inwhich users of a site I am building need the
Hopefully someone out there can help me with this.....I'm building an MVC3 project and
I'm having an issue on a site I'm building where the datatable is not
I used this brilliant solution to convert a linq query to a datatable. But
Building a stand alone WPF application that uses a MSSQL backend. I would like
Building a new Mobile Web Platform for Mobile Users to purchase & download content

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.