Results 1 to 20 of 20

Thread: Sorting Order

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2011
    Posts
    688

    Sorting Order

    Hi

    I have items with code as alphanumeric . For e.g I1,I2,I3,I4,I5,I6,I7,I8,I9,I10,I11,....I20,I21.

    In listview i am displaying code & name. When i click on code it sorts like this I1,I10,I11,I2 whereas i want it should display like this I1,I2, & so on.

    Thanks

  2. #2
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    9,017

    Re: Sorting Order

    Simple:-
    vbnet Code:
    1. '
    2.         Dim values As String() = New String() {"I1", "I23", "I14", "I12", "I1", "I3", "I11"}
    3.  
    4.         values = values.OrderBy(Function(s) CInt(s.Substring(1))).ToArray

    EDIT

    Oh God...I just realized this was a VB6 question and you also implied that you have a sort already. Well the idea in the above code is that you sort on the numbers by extracting them from each of the strings and converting them into numbers.

    How are you sorting ? Do you have some routine that sorts ? If so, post it up.
    Last edited by Niya; Oct 16th, 2013 at 04:44 AM.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2011
    Posts
    688

    Re: Sorting Order

    Hi Niya

    Records can be in hundreds or thousands

    Thanks

  4. #4
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    9,017

    Re: Sorting Order

    Did you see my post edit ?
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  5. #5
    Frenzied Member
    Join Date
    Mar 2008
    Posts
    1,210

    Re: Sorting Order

    At the moment you are getting an alphabetical sort such as you would get in a dictionary of words.
    To sort 'codes' like these numerically you should pad them out to equal length; this is typically done with leading zeroes as in;

    0I1,0I2,0I3,0I4,0I5,0I6,0I7,0I8,0I9,I10,I11,....I20,I21

    but you can use any character you wish.

    Oooops!
    Correction: I01,I02,I03,I04,I05,I06,I07,I08,I09,I10,I11,....I20,I21
    Last edited by Magic Ink; Oct 17th, 2013 at 05:40 PM. Reason: Oooops!

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2011
    Posts
    688

    Re: Sorting Order

    Hi

    This is the code

    Dim itmX As ListItem
    Dim intcount As Integer

    lvwitem.ListItems.Clear
    For intcount = 0 To rstitem.RecordCount - 1
    Set itmX = lvwitem.ListItems.Add(, "A" & rstitem("icode"))
    itmX.Text = rstitem("icode")
    itmX.SubItems(1) = rstitem("idesc")
    rstitem.MoveNext
    Next intcount
    If rstitem.RecordCount > 0 Then
    rstitem.MoveFirst
    End If

    rsitem is Items table recordset

    Thanks

  7. #7
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    9,017

    Re: Sorting Order

    Wait a minute, you're getting this from a database ? What SQL Select statement are you using to fill that RecordSet ? You might just be able to palm that sort off to database engine which is far better at sorting than any implementation you can provide.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2011
    Posts
    688

    Re: Sorting Order

    Hi Niya

    It is like this

    rstitem.Open "select * from itemmaster", cnn1, adOpenStatic, adLockOptimistic

    Thanks

  9. #9
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    9,017

    Re: Sorting Order

    Use this Select statement instead:-
    SQL Code:
    1. select * from itemmaster order by Cast(substring(icode,2,len(icode) - 1) as int)

    I'm assuming the column in question is icode and the database is SQL Server.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  10. #10

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2011
    Posts
    688

    Re: Sorting Order

    Hi

    It is giving me error Undefined function cast in expression
    rstitem.Open "SELECT * FROM itemmaster ORDER BY Cast(substring(icode,2,len(icode) - 1))", cnn1, adOpenStatic, adLockOptimistic

    Thanks

  11. #11
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    9,017

    Re: Sorting Order

    What database are you using ?
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  12. #12

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2011
    Posts
    688

    Re: Sorting Order

    Hi

    I am using MS-Access

    Thanks

  13. #13
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    9,017

    Re: Sorting Order

    Use this instead then:-
    sql Code:
    1. SELECT * FROM itemmaster ORDER BY CLng(Mid(icode,2,len(icode) - 1))
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  14. #14
    PowerPoster
    Join Date
    Jun 2013
    Posts
    7,454

    Re: Sorting Order

    Quote Originally Posted by Niya View Post
    Simple:-
    vbnet Code:
    1. '
    2.         Dim values As String() = New String() {"I1", "I23", "I14", "I12", "I1", "I3", "I11"}
    3.  
    4.         values = values.OrderBy(Function(s) CInt(s.Substring(1))).ToArray

    Oh God...I just realized this was a VB6 question ...
    Yeah - sure ...

    Here's some similar thing in VB6, using vbRichClient5:

    Code:
    Dim Arr As cArrayList
    Set Arr = New_c.ArrayList(vbString, "XZ 1", "XA 1", "I23", "I14", "I2", "I1", "I3", "X-11", "X-1")
        Arr.Sort cmpLogical
    then followed by a Print-Out of contents:
    Code:
    Dim i As Long
    For i = 0 To Arr.Count - 1
      Debug.Print Arr(i)
    Next
    gives the following "logically" sorted List:
    I1
    I2
    I3
    I14
    I23
    X-1
    X-11
    XA 1
    XZ 1

    Olaf

  15. #15

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2011
    Posts
    688

    Re: Sorting Order

    Hi Niya

    Recordset is o.k

    rstitem.Open "SELECT * FROM itemmaster ORDER BY val(Mid(icode,2,len(icode) - 1))", cnn1, adOpenStatic, adLockOptimistic

    But in Listview it is not displaying as per recordset


    Dim itmX As ListItem
    Dim intcount As Integer

    lvwitem.ListItems.Clear
    For intcount = 0 To rstitem.RecordCount - 1
    Set itmX = lvwitem.ListItems.Add(, "A" & rstitem("icode"))
    itmX.Text = rstitem("icode")
    itmX.SubItems(1) = rstitem("idesc")
    rstitem.MoveNext
    Next intcount
    If rstitem.RecordCount > 0 Then
    rstitem.MoveFirst
    End If


    Thanks

  16. #16
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    9,017

    Re: Sorting Order

    Quote Originally Posted by Schmidt View Post
    Yeah - sure ...

    Here's some similar thing in VB6, using vbRichClient5:

    Code:
    Dim Arr As cArrayList
    Set Arr = New_c.ArrayList(vbString, "XZ 1", "XA 1", "I23", "I14", "I2", "I1", "I3", "X-11", "X-1")
        Arr.Sort cmpLogical
    then followed by a Print-Out of contents:
    Code:
    Dim i As Long
    For i = 0 To Arr.Count - 1
      Debug.Print Arr(i)
    Next
    gives the following "logically" sorted List:
    I1
    I2
    I3
    I14
    I23
    X-1
    X-11
    XA 1
    XZ 1

    Olaf
    I was wondering how long it would take you to bite

    Quote Originally Posted by Jagjit View Post
    Hi Niya...Recordset is o.k.....But in Listview it is not displaying as per recordset
    lol one problem at a time....Did my altered Select query sort according to your need ? You didn't confirm that.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  17. #17

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2011
    Posts
    688

    Re: Sorting Order

    Hi Niya

    There is no problem in select statement . It gives me correct results when i see the results in below code using msgbox that is also correct but problem is when it displays in Listview it is not correct.

    lvwitem.ListItems.Clear
    For intcount = 0 To rstitem.RecordCount - 1
    Set itmX = lvwitem.ListItems.Add(, "A" & rstitem("icode"))
    itmX.Text = rstitem("icode")
    itmX.SubItems(1) = rstitem("idesc")
    rstitem.MoveNext
    Next intcount
    If rstitem.RecordCount > 0 Then
    rstitem.MoveFirst
    End If

    Thanks

  18. #18
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    9,017

    Re: Sorting Order

    So this is another problem entirely ?
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  19. #19
    Frenzied Member
    Join Date
    Jan 2009
    Location
    Watch Window(Shift+f9)
    Posts
    1,879

    Re: Sorting Order

    Hi ,Can you post the screenshot of msgbox . any way can you try the following way .
    Code:
    rstitem.Open "SELECT * FROM itemmaster ORDER BY val(Mid(icode,2,len(icode) - 1))", cnn1, adOpenStatic, adLockOptimistic
    
    If rsitem.state=adstateopen then
     if not (rsitem.eof and rsitem.bof) then 'if record found then
    
    Dim itmX As ListItem
    Dim intcount As Integer
    
    lvwitem.ListItems.Clear
    For intcount = 0 To rstitem.RecordCount - 1
    Set itmX = lvwitem.ListItems.Add(, "A" & rstitem("icode"))
    itmX.Text = rstitem("icode")
    itmX.SubItems(1) = rstitem("idesc")
    rstitem.MoveNext
    Next intcount 
    'If rstitem.RecordCount > 0 Then 'it does not seems to check something found then Moving pointer at very first record of the recordset
    'rstitem.MoveFirst
    'End If
    end if
    End if

  20. #20
    New Member
    Join Date
    Jul 2013
    Posts
    8

    Re: Sorting Order

    Since you are using a personalized sorting in the SQL Query, you must ensure that the Sorted Property of the ListView Control is False (other way the listview will re-sort in the normal text string mode and you will have the list incorrectly sorted again).

    Code:
    lvwItem.Sorted = False

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width