Solved

Sort LINQ result, integers first then strings

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
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…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now