Solved

Sort LINQ result, integers first then strings

Posted on 2010-09-13
3
463 Views
Last Modified: 2013-11-11
Hi all

I am getting back results from a LINQ query. The results are gathered from an object with its own members and public properties which in turn gets its data from the DB. This result is loaded into a combobox I create in code and is attached to a datagridview.

The issue is that it is a combination of integers and strings, such as the following:

1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14, 15+, not answered.

The problem is, that with this combination it is sorted as a list of strings. Like this:

1, 10, 11, 12, 13, 14, 15+, 2, 3, 4, 5, 6, 7, 8, 9, not answered.

Is there any way to sort the list so that the integers come first and then the strings, such as the top list indicates?

Thank you
0
Comment
Question by:culminIT
  • 2
3 Comments
 
LVL 29

Expert Comment

by:Gautham Janardhan
ID: 33660369

 var temp = "1,+,3,10,5,6,7,8,9,4,11,12,13,14,15,2".Split(",".ToCharArray()).ToList();
var temp2 = temp.OrderBy(new Func<string, int>(orderBy)).ToList();


public int orderBy(string b)
        {
            int value;

            if (int.TryParse(b, out value))
            {
                return value;
            }
            else
            {
                return int.MaxValue;
            }
        }

Open in new window

0
 
LVL 29

Expert Comment

by:Gautham Janardhan
ID: 33660433
oops forgot it was VB find VB variant below
Sub Main()
        Dim temp As List(Of String) = "1,+,3,10,5,6,7,8,9,4,11,12,13,14,15,2".Split(",".ToCharArray()).ToList()
        Dim temp2 As List(Of String) = temp.OrderBy(Function(arrayValue) orderBy(arrayValue))
    End Sub


    Private Function orderBy(ByVal value1 As String) As Boolean
        Dim value As Integer
        If (Integer.TryParse(value1, value)) Then
            Return value
        Else
            Return Integer.MaxValue
        End If
    End Function

Open in new window

0
 

Accepted Solution

by:
culminIT earned 0 total points
ID: 33660451
Hi all

Sorry to have posted the question but I looked at it in the meantime and worked out an answer. Hopefully this will help other people as well.

I created another dummy column called sortOrder. This is a integer field.

When the data is read into the member, it checks whether it is numeric or not. If it is numeric, the sortOrder value is assigned the integer value of the result returned. (So if the value is 11, the sortOrder value is 11).

If the value returned is a string, the sortOrder value is given a sentinal value, such as 100,000 .

Afterwards you just sort according to the sortOrder(integer) and then the actual value (to ensure that even strings are sorted correctly).

This ensures that the numbers are first and strings second.

I hope this makes sense.

Regards
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Visual studio 2015 1 34
Selenium: Transitioning from unit testing to automated testing 4 58
Problem to Office 1 40
PowerShell: ForEach-Object Export to CSV 4 37
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question