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 969917
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T02:43:20+00:00 2026-05-16T02:43:20+00:00

I have a custom sort routine that works great, but I was curious about

  • 0

I have a custom sort routine that works great, but I was curious about how I would convert it to use less code with LINQ or lambda. SortByGrade sorts a string value representing a grade ( K – 12 ) so that it returns: K, 1, 2, 3, 4, etc. instead of 1, 10, 2, etc

    /// <summary>
    /// Sorts a grade ( K - 12 ) numerically
    /// </summary>
    /// <remarks>A sort column is added and then sorted</remarks>
    /// <param name="table">table to sort</param>
    /// <param name="columnToSort">column to sort</param>
    /// <param name="sortDirection">enum of either asc or desc</param>
    /// <returns>Sorted data table</returns>
    public static DataTable SortByGrade(DataTable table, string columnToSort, SortDirection sortDirection)
    {
        string tmpSortColumn = "TempSortColumn";
        table.Columns.Add(tmpSortColumn, typeof(int));

        foreach (DataRow row in table.Rows)
        {
            row[tmpSortColumn] = ConvertGrade(row[columnToSort].ToString());
        }

        // sort on our need sort column
        table.DefaultView.Sort = String.Format("{0} {1}", tmpSortColumn, sortDirection.ToString());

        // return the sorted table
        return table.DefaultView.ToTable();
    }

and sort helper:

private static int ConvertGrade(string grade)
    {   
        int convertedGrade;
        switch(grade.ToUpper().Trim())
        {
            case "K":
                convertedGrade = 0;
                break;
            case "1":
                convertedGrade = 1;
                break;
            case "2":
                convertedGrade = 2;
                break;
            case "3":
                convertedGrade = 3;
                break;
            case "4":
                convertedGrade = 4;
                break;
            case "5":
                convertedGrade = 5;
                break;
            case "6":
                convertedGrade = 6;
                break;
            case "7":
                convertedGrade = 7;
                break;
            case "8":
                convertedGrade = 8;
                break;
            case "9":
                convertedGrade = 9;
                break;
            case "10":
                convertedGrade = 10;
                break;
            case "11":
                convertedGrade = 11;
                break;
            case "12":
                convertedGrade = 12;
                break;
            // TODO: Remove these cases when the data is cleaned up
            case "00":
            case "17":
                convertedGrade = 100;
                break;
            default:
                convertedGrade = 0;
                break;
        }
return convertedGrade;
    }

Thanks so much.

  • 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-16T02:43:21+00:00Added an answer on May 16, 2026 at 2:43 am

    You can use DataTableExtensions for the AsEnumerable and CopyToDataTable methods.

    public static DataTable SortByGrade(DataTable table,
      string columnToSort, SortDirection sortDirection) 
    {
    
      IEnumerable<DataRow> query = 
        from row in table.AsEnumerable()
        orderby ConvertGrade(row[columnToSort].ToString())
        select row;
    
      if (sortDirection == "DESC")
      {
        query = query.Reverse();
      }
    
      DataTable result = query.CopyToDataTable();
      return result;
    }
    

    and sort helper:

    private static int ConvertGrade(string grade)  
    {
      string g = grade.ToUpper().Trim();
      int convertedGrade =
        g == "K" ? 0 :
        g == "1" ? 1 :
        g == "2" ? 2 :
        g == "3" ? 3 :
        g == "4" ? 4 :
        g == "5" ? 5 :
        g == "6" ? 6 :
        g == "7" ? 7 :
        g == "8" ? 8 :
        g == "9" ? 9 :
        g == "10" ? 10 :
        g == "11" ? 11 :
        g == "12" ? 12 :
    // TODO: Remove these cases when the data is cleaned up
        g == "00" ? 100 :  
        g == "17" ? 100 :
        0;  
    
      return convertedGrade;  
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have custom event that has several different subscribers who will all use the
I have a custom type I'd like to use with QVariant but I don't
I have some custom objects and dictionaries that I want to sort. I want
I have the following code I'd like to be used to custom sort my
I'm using the code from: http://www.swissdelphicenter.ch/torry/showcode.php?id=1103 to sort my TListView, which works GREAT on
Possible Duplicate: mysql custom sort I have an array of IDs: $ids = array(5,
I have a custom, multi-column sort attached to my jqGrid instance by means of
I am trying to add a dropdown to sort my custom posts. I have
I have custom classes that I currently instantiate within App.xaml as resources. I want
I have custom gallery. Gallery represents items that are frame layout. There are one

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.