Click here to Skip to main content
15,892,746 members
Articles / Programming Languages / C#

String Extension Method - Mixed Alpha/Numeric Sort Order

Rate me:
Please Sign up or sign in to vote.
4.92/5 (9 votes)
17 Feb 2009CPOL7 min read 80.3K   753   18  
Uses .NET extension method as an example on how to sort mixed alpha/numeric text
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TrentTobler.AlphaNumericSort;

namespace AlphaNumericSort
{
    public partial class TestForm : Form
    {
        System.ComponentModel.BindingList<TestRow> textList;

        public TestForm()
        {
            InitializeComponent();
        }

        private void TestForm_Load( object sender, EventArgs e )
        {
            this.textList = new BindingList<TestRow>();
            string[] sampleList = new string[]
            {
                "101 Dalmations",
                "95 Balloons",
                "Front 242",
                "Front 3 Rows",
                "12-31-2008",
                "1-4-2009",
                "2-17-2009",
                "Rev 13.2",
                "Rev 2.8B",
                "Rev 02.08A",
                "0xDeadBeef",
                "0x Dead Beef",
                "0xeat",
                "0",
                "00",
                "000",
                "2",
                "02",
                "002",
                "10",
                "010",
                "0010",
            };
            foreach( string s in sampleList )
            {
                this.textList.Add( new TestRow
                {
                    Text = s
                } );
            }

            this.textList.AllowEdit = true;
            this.textList.AllowNew = true;
            this.textList.AllowRemove = true;
            this.dataGridView.DataSource = this.textList;
        }

        private void ResortData( Func<string,string> getToken )
        {
            // set the sort token for all the rows (so token can be examined.)
            foreach( TestRow row in this.textList )
            {
                row.SortToken = getToken( row.Text );
            }

            // Sort the list.
            List<TestRow> newOrder = new List<TestRow>( from row in this.textList
                                                        orderby row.SortToken
                                                        select row );
            for( int i = 0; i < newOrder.Count; ++i )
            {
                this.textList[i] = newOrder[i];
            }
        }

        private void sortButton_Click( object sender, EventArgs e )
        {
            ResortData( x => x.GetAlphaNumericOrderToken( this.hexCheckBox.Checked, this.zeroPadCheckBox.Checked ) );
        }

        private void standardLexicalSortButton_Click( object sender, EventArgs e )
        {
            ResortData( x => x );
        }
    }

    public class TestRow
    {
        #region Fields

        string text = null;
        string sortToken = null;

        #endregion

        #region Properties

        public string Text
        {
            get
            {
                return this.text;
            }
            set
            {
                this.text = value;
            }
        }

        public string SortToken
        {
            get
            {
                return this.sortToken;
            }
            set
            {
                this.sortToken = value;
            }
        }

        #endregion
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions