locked
c# linq orderby orders incorrectly or not in number seqence RRS feed

  • Question

  • Hi

    My numbers in my List do not order in strict sequence when using c# Linq. I can see a pattern but the numbers are not in the exact order that I need.

    This is my c# Link code which has an orderby inside it ok :-

                //The Linq code which has an orderby within
                var cdbips = from cdb in CDBs
                             join ip in IPs
                             on cdb.Cdbno equals ip.Cdbno
                             where (cdb.Country == "Italy
                             orderby cdb.Cdbno ascending
                             select new classIP(cdb.Cdbno, ip.Ipno);
                foreach (var cdbip in cdbips)
                {
                   CDBIPs.Add(cdbip);
                }
                DisplayCDBIPs();
            }
            private void DisplayCDBIPs()
            {
                listViewCDBIPs.Items.Clear();
                for (int i = 0; i < CDBIPs.Count; i++)
                {
                    ListViewItem objListItem = listViewCDBIPs.Items.Add(CDBIPs[i].Cdbno);
                    objListItem.SubItems.Add(CDBIPs[i].Ipno);
                }
            }

    The output I see is :-

    Thanks to anyone who can advise or help

    Cheers

    Matt

    Friday, October 19, 2012 1:26 PM

Answers

  • Hi Matt,

    The field you are ordering on probably is a string field, right? In string representation, 8 precedes 9, so 844569654 precedes 91. There are several things you can do:

    • Treat the numbers as numbers, not as strings
    • Pad all the numbers on the left hand side with zeros until they have the same length

    Hope this helps


    Please mark the best replies as answers - Twitter: @rickvdbosch - Blog: http://bloggingabout.net/blogs/rick

    • Proposed as answer by Norkk Friday, October 19, 2012 1:41 PM
    • Marked as answer by jmatty2000 Friday, October 19, 2012 1:58 PM
    Friday, October 19, 2012 1:39 PM
  • Do what Rick tell you, like this

                var cdbips = from cdb in CDBs
                             join ip in IPs
                             on cdb.Cdbno equals ip.Cdbno
                             where cdb.Country == "Italy"
                             orderby Convert.ToInt32(cdb.Cdbno) ascending
                             select new classIP(cdb.Cdbno, ip.Ipno);


    If you get your question answered, please come back and Alternate TextMark As Answer.
    Web Developer



    • Edited by Norkk Friday, October 19, 2012 1:46 PM
    • Marked as answer by jmatty2000 Friday, October 19, 2012 1:58 PM
    Friday, October 19, 2012 1:46 PM

All replies

  • hi Norkk

    Look at number 996 - it not in numeric order. I hope you can see what I mean now.

    The order I would expect is as in the listview screenshot above but the 96 should be displayed at the top

    Thanks

    Matt

    Friday, October 19, 2012 1:32 PM
  • Yeah, i was not see that one, but i tried this example

                int[] arr = new int[] { 9601, 9663, 9738, 9777, 9801, 9817, 996, 9978 };
    
                var query = from row in arr
                            orderby row ascending
                            select row ;
                foreach (var n in query)
                    Console.WriteLine(n);

    Or this one

                var query = arr.OrderBy(row => row);
                foreach (var n in query)
                    Console.WriteLine(n);

    And the output was the correct, 996 come at first.


    If you get your question answered, please come back and Alternate TextMark As Answer.
    Web Developer

    Friday, October 19, 2012 1:36 PM
  • Hi Matt,

    The field you are ordering on probably is a string field, right? In string representation, 8 precedes 9, so 844569654 precedes 91. There are several things you can do:

    • Treat the numbers as numbers, not as strings
    • Pad all the numbers on the left hand side with zeros until they have the same length

    Hope this helps


    Please mark the best replies as answers - Twitter: @rickvdbosch - Blog: http://bloggingabout.net/blogs/rick

    • Proposed as answer by Norkk Friday, October 19, 2012 1:41 PM
    • Marked as answer by jmatty2000 Friday, October 19, 2012 1:58 PM
    Friday, October 19, 2012 1:39 PM
  • Do what Rick tell you, like this

                var cdbips = from cdb in CDBs
                             join ip in IPs
                             on cdb.Cdbno equals ip.Cdbno
                             where cdb.Country == "Italy"
                             orderby Convert.ToInt32(cdb.Cdbno) ascending
                             select new classIP(cdb.Cdbno, ip.Ipno);


    If you get your question answered, please come back and Alternate TextMark As Answer.
    Web Developer



    • Edited by Norkk Friday, October 19, 2012 1:46 PM
    • Marked as answer by jmatty2000 Friday, October 19, 2012 1:58 PM
    Friday, October 19, 2012 1:46 PM
  • I like your answers!!! The key was that I was doing an orderby on strings. Shame Microsoft doesn't give the flexibility to order string numbers. But I'm still gratefull as I'm using the free Express version at the moment.

    cheers

    Matt

    Friday, October 19, 2012 1:57 PM
  • I like your answers!!! The key was that I was doing an orderby on strings. Shame Microsoft doesn't give the flexibility to order string numbers. But I'm still gratefull as I'm using the free Express version at the moment.

    It does, you just need to parse the strings into actual numbers before sorting on them.  If you didn't, then it would have no way of handling non-numeric strings.
    Friday, October 19, 2012 2:10 PM