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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T07:00:28+00:00 2026-05-11T07:00:28+00:00

I’ve got a report being built from a dataset. The dataset uses the Sort

  • 0

I’ve got a report being built from a dataset. The dataset uses the Sort property to order the data. I know that I can create a sort expression like this:

‘field desc, field2 asc’

But what I need now is a way to do a custom sort. In SQL, I can perform a custom sort by doing something like this:

order by      case when field = 'Some Value' then 0 end     case when field = 'Another Value' then 1 end 

To basically re-define my sort (i.e, Some Value comes before Another Value).

Is it possible to do something similar as a sort expression against a DataView?

  • 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. 2026-05-11T07:00:28+00:00Added an answer on May 11, 2026 at 7:00 am

    Ok, I just whipped this up real quick, and didn’t do all the neccessary error handling and null checking, but it should give you an idea and should be enough to get you started:

    public static class DataTableExtensions {     public static DataView ApplySort(this DataTable table, Comparison<DataRow> comparison)     {          DataTable clone = table.Clone();         List<DataRow> rows = new List<DataRow>();         foreach (DataRow row in table.Rows)         {             rows.Add(row);             }          rows.Sort(comparison);          foreach (DataRow row in rows)         {             clone.Rows.Add(row.ItemArray);         }          return clone.DefaultView;     }   } 

    Usage:

        DataTable table = new DataTable();     table.Columns.Add('IntValue', typeof(int));     table.Columns.Add('StringValue');      table.Rows.Add(11, 'Eleven');     table.Rows.Add(14, 'Fourteen');     table.Rows.Add(10, 'Ten');     table.Rows.Add(12, 'Twelve');     table.Rows.Add(13, 'Thirteen'); 

    //Sort by StringValue:

     DataView sorted = table.ApplySort((r, r2) =>         {             return ((string)r['StringValue']).CompareTo(((string)r2['StringValue']));         }); 

    Result:

    11 Eleven

    14 Fourteen

    10 Ten

    13 Thirteen

    12 Twelve

    //Sort by IntValue:

    DataView sorted = table.ApplySort((r, r2) =>             {                 return ((int)r['IntValue']).CompareTo(((int)r2['IntValue']));             }); 

    Result:

    10 Ten

    11 Eleven

    13 Thirteen

    12 Twelve

    14 Fourteen

    EDIT: Changed it to extension method.

    Now in your Lambda, (or you can create a full blown Comparison method) you can do any kind of custom sorting logic that you need. Remember, -1 is less than, 0 is equal to, and 1 is greater than.

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

Sidebar

Ask A Question

Stats

  • Questions 274k
  • Answers 274k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can set the ConnectionModifier property of the TableAdapter in… May 13, 2026 at 2:16 pm
  • Editorial Team
    Editorial Team added an answer Try this file out. Let me know if you have… May 13, 2026 at 2:16 pm
  • Editorial Team
    Editorial Team added an answer in Windows Registry as encrypted values, or in Application Data… May 13, 2026 at 2:16 pm

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have a French site that I want to parse, but am running into
I have text I am displaying in SIlverlight that is coming from a CMS

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.