Solved

Sort LINQ result, integers first then strings

Posted on 2010-09-13
3
460 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

786 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