CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: Random Sort

  1. #1
    Join Date
    Jan 2009
    Posts
    177

    Random Sort

    Hi all,

    Good day. I am facing a problem here in sorting. I has a list of ID selected from database. The list of ID is the following:

    AH
    AJ
    AP
    AW
    AZ

    How can I sort the ID where the ID that I select should be always on top, then only sort by ascending?

    For example, I select AP. So the sorting should be like this:
    AP, AH,AJ,AW,AZ

  2. #2
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Random Sort

    p,h,j,w.z ?? Not alphabetical...
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Jun 2005
    Location
    JHB South Africa
    Posts
    3,772

    Re: Random Sort

    loop through the list and find your item, and place it in the first position, then pass the list to a sort routine that has been set to sort from the second item...

    IE: Classic sort
    Code:
    For Loop1 = 2 to List.Length -1 ' Traditional is 1 to Length -1
        For Loop2 = Loop1 +1 to length
            if List(Loop1) < List(Loop2) then Swap ( List(Loop1) , List(Loop2))
        Next Loop2
    Next Loop1
    Articles VB6 : Break the 2G limit - Animation 1, 2 VB.NET : 2005/8 : Moving Images , Animation 1 , 2 , 3 , User Controls
    WPF Articles : 3D Animation 1 , 2 , 3
    Code snips: VB6 Hex Edit, IP Chat, Copy Prot., Crop, Zoom : .NET IP Chat (V4), Adv. ContextMenus, click Hotspot, Scroll Controls
    Find me in ASP.NET., VB6., VB.NET , Writing Articles, My Genealogy, Forum
    All VS.NET: posts refer to VS.NET 2008 (Pro) unless otherwise stated.

  4. #4
    Join Date
    Aug 2009
    Location
    NW USA
    Posts
    173

    Re: Random Sort

    Remove the selected item from your list and hold it. Order the remaining list then Insert the held item at position zero

  5. #5
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Random Sort

    Probably want to reverse the outer loop.
    Code:
    For Loop1 = List.Length -1 to 2 Step -1
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

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

Featured