Solved

# Sort LINQ result, integers first then strings

Posted on 2010-09-13
466 Views
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
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
• 2

LVL 29

Expert Comment

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;
}
}
``````
0

LVL 29

Expert Comment

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
``````
0

Accepted Solution

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

Question has a verified solution.

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

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
###### Suggested Courses
Course of the Month5 days, 3 hours left to enroll