views:

117

answers:

2

I am trying to sort a xml using linq queries using the following code

Dim SortedFields = From field In feedXML.Descendants("fields")
Dim sFieldList = From field In SortedFields.Descendants("field") Order By field.@position

The xml is shown below, but the issue is it is ordering it in the following order 1,10,11,12,13,14...19,2,3,4,5,6,7 instead of 1,2,3,4,5....10,11,12,.....19.

Can anybody help me sorting it in the correct order?

<fields>
    <field position="'6'" tablename="Students" headername="6" fieldreference="ClientField1" orderbydirection="ASC" displaycolumn="T" />
    <field position="'7'" tablename="Students" headername="7" fieldreference="ClientField2" orderbydirection="ASC" displaycolumn="T" />
    <field position="'8'" tablename="Students" headername="8" fieldreference="ClientField3" orderbydirection="ASC" displaycolumn="T" />
    <field position="'9'" tablename="Students" headername="9" fieldreference="ClientField4" orderbydirection="ASC" displaycolumn="T" />
    <field position="'10'" tablename="Students" headername="10" fieldreference="ClientField5" orderbydirection="ASC" displaycolumn="T" />
    <field position="'1'" tablename="Students" headername="1" fieldreference="ClientField6" orderbydirection="ASC" displaycolumn="T" />
    <field position="'2'" tablename="Students" headername="2" fieldreference="ClientField7" orderbydirection="ASC" displaycolumn="T" />
    <field position="'3'" tablename="Students" headername="3" fieldreference="ClientField8" orderbydirection="ASC" displaycolumn="T" />
    <field position="'4'" tablename="Students" headername="4" fieldreference="ClientField9" orderbydirection="ASC" displaycolumn="T" />
    <field position="'5'" tablename="Students" headername="5" fieldreference="ClientField10" orderbydirection="ASC" displaycolumn="T" />
    <field position="'11'" tablename="Students" headername="11" fieldreference="ClientField11" orderbydirection="ASC" displaycolumn="T" />
    <field position="'12'" tablename="Students" headername="12" fieldreference="ClientField12" orderbydirection="ASC" displaycolumn="T" />
    <field position="'13'" tablename="Students" headername="13" fieldreference="ClientField13" orderbydirection="ASC" displaycolumn="T" />
    <field position="'14'" tablename="Students" headername="14" fieldreference="ClientField14" orderbydirection="ASC" displaycolumn="T" />
    <field position="'15'" tablename="Students" headername="15" fieldreference="ClientField15" orderbydirection="ASC" displaycolumn="T" />
    <field position="'16'" tablename="Students" headername="16" fieldreference="ClientField16" orderbydirection="ASC" displaycolumn="T" />
    <field position="'17'" tablename="Students" headername="17" fieldreference="ClientField17" orderbydirection="ASC" displaycolumn="T" />
    <field position="'18'" tablename="Students" headername="18" fieldreference="ClientField18" orderbydirection="ASC" displaycolumn="T" />
    <field position="'19'" tablename="Students" headername="19" fieldreference="ClientField19" orderbydirection="ASC" displaycolumn="T" />
</fields>
+4  A: 

As Paul Tomblin points out this is alphabeticical ordering. If position is going to always be a number then you can cast it into an integer using (int?)

Dim SortedFields = From field In feedXML.Descendants("fields")
                            Dim sFieldList = From field In SortedFields.Descendants("field") Order By (int?)field.@position
stimms
Can you really do a conversion just like that in VB?
Jon Skeet
Awesome...That worked...Thanks a ton!
Mithil Deshmukh
Order By Integer.Parse(field.@position) is the right expression..Thanks again!
Mithil Deshmukh
DirectCast() or CInt() would be a more direct translation: they are operators, but Integer.Parse() is a function call.
Joel Coehoorn
+3  A: 

It's sorting in alphabetical order. You need to convert the position attribute to an integer in your Order By clause:

Dim SortedFields = From field In feedXML.Descendants("fields")
Dim sFieldList = From field In SortedFields.Descendants("field") _
    Order By CType(field.@position, Integer)

(Apologies if there's a better conversion available... that's the code suggested by the XAttribute conversion operator page)

Jon Skeet
How to convert it to integer?
Mithil Deshmukh
See my edited answer.
Jon Skeet