This is a migrated thread and some comments may be shown as answers.

Natural Sort

3 Answers 209 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Clint Singer
Top achievements
Rank 1
Clint Singer asked on 10 Aug 2011, 08:56 PM
Hi,

I was wondering if there is some way to do a "natural sort" on my data in the report.  The bulk of my columns are text strings that should also be numerically sorted.

ie.

Item 1
Item 10
Item 2

should be

Item 1
Item 2
Item 10

Any recommendations how I can accomplish what I need to do?  I should also point out that I also have nested sorts so have the data pre-sorted doesn't work.

Ideally it would be great if there was some way to hook onto a callback or pass in an IComparer which does the sorting logic.  This would be true of the grid control too.

Cheers,
Clint

3 Answers, 1 is accepted

Sort by
0
Steve
Telerik team
answered on 12 Aug 2011, 02:35 PM
Hello Clint,

There are quite a few options to accomplish what you want:
  1. create a Calculated Field which would serve as index for your sorting needs i.e. you would apply a sorting over this field.
  2. utilize an User Function and handle the sorting yourself in your favorite language.
  3. utilize built-in SubStr function to get the number, convert it to int with CInt function and apply sorting.

Greetings,
Steve
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get now >>

0
Clint Singer
Top achievements
Rank 1
answered on 22 Aug 2011, 10:00 PM
Hi,

Could you please elaborate a bit more on the User Function solution.  I am not clear on what I need to pass as parameters, what I should send as return values nor how to use it in an expression with respect to how the sorting is expected to work.

I don't think the other options would work for my scenario as it is a bit more complicated than the example I had posted.

Cheers,
Clint
0
Steve
Telerik team
answered on 23 Aug 2011, 08:34 AM
Hello Clint,

It all depends on your exact scenario, which you just said differs from your example, so without a concrete task, we cannot provide concrete solution. Generally a single parameter should suffice for your needs e.g.

= SetUpSorting(Fields.MyField)

where Fields.MyField is the field containing your string. Thus you can get the number part from your string, convert it to integer and return appropriate result by which you can sort. Here is pseudo user function:

public static int SetUpSorting(string value)
      {
          //code to get number part from value
                               
          return numberValue;
      }

and use the above expression as sorting expression.

Best wishes,
Steve
the Telerik team

Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>

Tags
General Discussions
Asked by
Clint Singer
Top achievements
Rank 1
Answers by
Steve
Telerik team
Clint Singer
Top achievements
Rank 1
Share this question
or