Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Bubble sort an array of Structure

Bubble sort an array of Structure

Scheduled Pinned Locked Moved Visual Basic
data-structuresquestion
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    amatbrewer
    wrote on last edited by
    #1

    I have an array of a structure similar to below: Public Structure MyStructure Public MyString as string Public MyInt1 as Int Public MyInt2 as int end structure Dim StructArray () I want a function that will sort the data by the MyString field. Any suggestions? Thanks in advance!

    David Wilkes

    T 1 Reply Last reply
    0
    • A amatbrewer

      I have an array of a structure similar to below: Public Structure MyStructure Public MyString as string Public MyInt1 as Int Public MyInt2 as int end structure Dim StructArray () I want a function that will sort the data by the MyString field. Any suggestions? Thanks in advance!

      David Wilkes

      T Offline
      T Offline
      TwoFaced
      wrote on last edited by
      #2

      You can use the sort method found in the Array class. You can implement IComparable in your structure. Basically this allows objects of type MyStructure to be compared. Here is a simple example.

      Public Class Form1
      Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
      Dim people As Person() = {New Person("Bob", "Smith"), New Person("Chris", "Adams")}
      Array.Sort(people)
      For Each p As Person In people
      Console.WriteLine(p.FirstName & ":" & p.LastName)
      Next
      End Sub
      End Class

      Public Structure Person
      Implements IComparable(Of Person)

      Public FirstName As String
      Public LastName As String
      
      Public Sub New(ByVal first As String, ByVal last As String)
          FirstName = first
          LastName = last
      End Sub
      
      Public Function CompareTo(ByVal other As Person) As Integer Implements System.IComparable(Of Person).CompareTo
          Return String.Compare(Me.LastName, other.LastName)
      End Function
      

      End Structure

      This will sort alphabetically by the LastName field. If you want to sort in descending order you could first call sort to put them in ascending order and then call array.reverse(YourArray) to put them in descedning order. There are other ways to allow for multiple different sorts. If you want to learn more about it try looking up the interfaces Icomparer, and Icomparable.

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups