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

  • SEARCH
  • Home
  • 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 6991329
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:29:08+00:00 2026-05-27T19:29:08+00:00

I have a requirement to sort the values of a data table column. That

  • 0

I have a requirement to sort the values of a data table column. That column contains strings, integers or mixed texts. For example:

The data table column contains values like this: 23, 18, 12, store 23, store a1, 1283, 25, ...

If I sort the values by using Dataview.sort() method it results in this order: 12, 1283, 18, 23, 25, store 1283, store a1, ... but I need like this: 12, 18, 23, 25, 1283, store 23, store a1, ...

Is there any simple method for attaining this requirement?

  • 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-05-27T19:29:08+00:00Added an answer on May 27, 2026 at 7:29 pm

    I think you should use natural sorting and make your own IComparer

    The best algo I found was here

    http://www.davekoelle.com/files/AlphanumComparator.cs.

    Just make it a generic class(as linq uses as Linq order by takes IComparer) , like following

    public class AlphanumComparator<T> : IComparer<T>
        {
            private enum ChunkType { Alphanumeric, Numeric };
            private bool InChunk(char ch, char otherCh)
            {
                ChunkType type = ChunkType.Alphanumeric;
    
                if (char.IsDigit(otherCh))
                {
                    type = ChunkType.Numeric;
                }
    
                if ((type == ChunkType.Alphanumeric && char.IsDigit(ch))
                    || (type == ChunkType.Numeric && !char.IsDigit(ch)))
                {
                    return false;
                }
    
                return true;
            }
    
            public int Compare(T x, T y)
            {
                String s1 = x as string;
                String s2 = y as string;
                if (s1 == null || s2 == null)
                {
                    return 0;
                }
    
                int thisMarker = 0, thisNumericChunk = 0;
                int thatMarker = 0, thatNumericChunk = 0;
    
                while ((thisMarker < s1.Length) || (thatMarker < s2.Length))
                {
                    if (thisMarker >= s1.Length)
                    {
                        return -1;
                    }
                    else if (thatMarker >= s2.Length)
                    {
                        return 1;
                    }
                    char thisCh = s1[thisMarker];
                    char thatCh = s2[thatMarker];
    
                    StringBuilder thisChunk = new StringBuilder();
                    StringBuilder thatChunk = new StringBuilder();
    
                    while ((thisMarker < s1.Length) && (thisChunk.Length == 0 || InChunk(thisCh, thisChunk[0])))
                    {
                        thisChunk.Append(thisCh);
                        thisMarker++;
    
                        if (thisMarker < s1.Length)
                        {
                            thisCh = s1[thisMarker];
                        }
                    }
    
                    while ((thatMarker < s2.Length) && (thatChunk.Length == 0 || InChunk(thatCh, thatChunk[0])))
                    {
                        thatChunk.Append(thatCh);
                        thatMarker++;
    
                        if (thatMarker < s2.Length)
                        {
                            thatCh = s2[thatMarker];
                        }
                    }
    
                    int result = 0;
                    // If both chunks contain numeric characters, sort them numerically
                    if (char.IsDigit(thisChunk[0]) && char.IsDigit(thatChunk[0]))
                    {
                        thisNumericChunk = Convert.ToInt32(thisChunk.ToString());
                        thatNumericChunk = Convert.ToInt32(thatChunk.ToString());
    
                        if (thisNumericChunk < thatNumericChunk)
                        {
                            result = -1;
                        }
    
                        if (thisNumericChunk > thatNumericChunk)
                        {
                            result = 1;
                        }
                    }
                    else
                    {
                        result = thisChunk.ToString().CompareTo(thatChunk.ToString());
                    }
    
                    if (result != 0)
                    {
                        return result;
                    }
                }
    
                return 0;
            }
    
    
        }
    

    Now to apply this, use linq

     DataTable dt = new DataTable();
                dt.TableName = "Sort";
                dt.Columns.Add("Check");
                DataRow dr = dt.NewRow();
                dr["Check"] = "12";
                dt.Rows.Add(dr);
    
                DataRow dr2 = dt.NewRow();
                dr2["Check"] = "1283";
                dt.Rows.Add(dr2);
    
                DataRow dr3 = dt.NewRow();
                dr3["Check"] = "store 1283";
                dt.Rows.Add(dr3);
    
                DataRow dr4 = dt.NewRow();
                dr4["Check"] = "23";
                dt.Rows.Add(dr4);
    
                DataView dv = new DataView();
                dv.Table = dt;
    
                AlphanumComparator<string> comparer = new AlphanumComparator<string>();
                //DataTable dtNew = dv.Table;
                DataTable dtNew = dv.Table.AsEnumerable().OrderBy(x => x.Field<string>("Check"), comparer).CopyToDataTable();
                dtNew.TableName = "NaturalSort";
    
                dv.Table = dtNew;
    

    Result 12, 23, 1283, store 1283

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have requirement like, suppose I have a 'property' table which has 'ListingKey' field
i have a requirement to build some sort of services that can easily be
I have a requirement to sort a collection of objects, They are shown on
In our project we have requirement that, after receiving sms message from third party
We have the requirement to take a form submission and save some data, then
I have the requirement to access the following data, where available, on mobile phones
My scenario is as follows: I have a table of data (handful of fields,
I have a requirement to create a dynamic table from an xml file similar
I have a requirement to keep a history of values of some fields in
I have a requirement to sort search results by relevance and another field. I

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.