Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Sort LINQ result, integers first then strings

Posted on 2010-09-13
3
Medium Priority
?
467 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

705 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