Results 1 to 4 of 4

Thread: [RESOLVED] [2008] Sort 3 arrays at once

  1. #1

    Thread Starter
    Member
    Join Date
    Nov 2007
    Posts
    62

    Resolved [RESOLVED] [2008] Sort 3 arrays at once

    I think I need to use a bubble sort or something of the like.

    Basically I need to sort a string array and have to integer arrays get sorted with it.

    I would use Array.Sort(myArray) but that would sort them all individually.

    Say I have

    3 4 bob
    2 9 stacey
    9 1 max

    I need this to be
    3 4 bob
    9 1 max
    2 9 stacey

    So the string is sorted alphabetically A to Z and the integers go with it.

    Thanks for any help and code.
    Last edited by rhijaen; Sep 28th, 2008 at 11:09 PM.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2008] Sort 3 arrays at once

    You could write your own routine to do it but, as long as you don't mind sorting the strings twice, you can do it with Array.Sort:
    vb.net Code:
    1. Dim strings1 As String() = {"bob", "stacey", "max"}
    2. Dim integers1 As Integer() = {3, 2, 9}
    3. Dim integers2 As Integer() = {4, 9, 1}
    4.  
    5. Dim strings2 As String() = DirectCast(strings1.Clone(), String())
    6.  
    7. Array.Sort(strings1, integers1)
    8. Array.Sort(strings2, integers2)
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Member
    Join Date
    Nov 2007
    Posts
    62

    Re: [2008] Sort 3 arrays at once

    Thanks, worked perfectly.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [RESOLVED] [2008] Sort 3 arrays at once

    Having said all of that, if possible it would be better to, rather than using three concurrent arrays, define a class or structure that has three properties: one String and two Integers. You can then have one array of that type and then sort it by the String property, which will inherently sort the Integers. Concurrent arrays are to be avoided in this day and age if possible.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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