|
-
May 14th, 2013, 01:11 PM
#1
Thread Starter
Addicted Member
[RESOLVED] Sorting a multidimensional array
I am hoping someone can point me in the right direction. I am trying to sort cities by population. The cities are stored in a multidimensional array from a file:
Example (using only 3 of the thousands of cities that are stored in the array):
Code:
Dim Cities(2, 2) as string
Cities(0, 0) = "Phoenix" '//City Name
Cities(0, 1) = "Arizona" '//City's State
Cities(0, 2) = "1469471" '//City's Population
Cities(1, 0) = "New York" '//City Name
Cities(1, 1) = "New York" '//City's State
Cities(1, 2) = "8244910" '//City's Population
Cities(2, 0) = "Los Angeles" '//City Name
Cities(2, 1) = "California" '//City's State
Cities(2, 2) = "3819702" '//City's Population
When sorted, I would like for them to be changed into this sequence (highest to lowest):
Code:
Cities(0, 0) = "New York" '//City Name
Cities(0, 1) = "New York" '//City's State
Cities(0, 2) = "8244910" '//City's Population
Cities(1, 0) = "Los Angeles" '//City Name
Cities(1, 1) = "California" '//City's State
Cities(1, 2) = "3819702" '//City's Population
Cities(2, 0) = "Phoenix" '//City Name
Cities(2, 1) = "Arizona" '//City's State
Cities(2, 2) = "1469471" '//City's Population
Would like to be able to call a function, sub or routine to sort the cities based on (x, 2) contents of the array (eg. population). I was hoping there might be a simple, straightforward way for doing this.
-
May 14th, 2013, 01:35 PM
#2
Thread Starter
Addicted Member
Re: Sorting a multidimensional array
After posting this, I did some research and found something that I modified to work with what I am trying to do. I have posted the code below for anyone that may need the same or a similar solution in the future:
Code:
Public Class Form1
Dim Cities(2, 2) As String
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Cities(0, 0) = "Phoenix" '//City Name
Cities(0, 1) = "Arizona" '//City's State
Cities(0, 2) = "1469471" '//City's Population
Cities(1, 0) = "New York" '//City Name
Cities(1, 1) = "New York" '//City's State
Cities(1, 2) = "8244910" '//City's Population
Cities(2, 0) = "Los Angeles" '//City Name
Cities(2, 1) = "California" '//City's State
Cities(2, 2) = "3819702" '//City's Population
End Sub
Public Sub SortCities()
Dim TempPos As Integer
For i = 0 To (UBound(Cities) - 1)
For n = i To UBound(Cities)
If Cities(n, 2) > Cities(i, 2) Then
TempPos = Cities(i, 2)
Cities(i, 2) = Cities(n, 2)
Cities(n, 2) = TempPos
End If
Next
Next
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim tmp_msg As String = ""
For i = 0 To UBound(Cities)
tmp_msg = tmp_msg & Cities(i, 0) & " | " & Cities(i, 1) & " | " & Cities(i, 2) & vbCrLf
Next
MsgBox(tmp_msg & "-----------------------------------------")
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
SortCities()
End Sub
End Class
-
May 14th, 2013, 03:07 PM
#3
Re: [RESOLVED] Sorting a multidimensional array
This is not the best way to do what you're wanting. What you should do is create a class that will represent your city, then store a collection of cities in a generic list(of <t>). From there you can sort much easier. Here is a quick example:
City class:
Code:
'Come on now, they should be on
Option Strict On
Option Explicit On
Public Class City
'City name, state, and population properties
Private _name As String
Public Property Name() As String
Get
Return _name
End Get
Set(ByVal value As String)
_name = value
End Set
End Property
Private _state As String
Public Property State() As String
Get
Return _state
End Get
Set(ByVal value As String)
_state = value
End Set
End Property
Private _population As Integer
Public Property Population() As Integer
Get
Return _population
End Get
Set(ByVal value As Integer)
_population = value
End Set
End Property
End Class
Form code:
Code:
Option Strict On
Option Explicit On
Public Class Form1
Private cities As New List(Of City)
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
'Declare 3 new cities
Dim pheonix, ny, la As New City
'Set their properties
With pheonix
.Name = "Pheonix"
.State = "Arizona"
.Population = 1469471
End With
With ny
.Name = "New York City"
.State = "New York"
.Population = 8244910
End With
With la
.Name = "Los Angeles"
.State = "California"
.Population = 3819702
End With
'Add them to the list
cities.AddRange({pheonix, ny, la})
End Sub
Private Sub btn_Ascending_Click(sender As System.Object, e As System.EventArgs) Handles btn_Ascending.Click
'Sort by population
cities = cities.OrderBy(Function(x) x.Population).ToList
'Display results in listbox
For Each place As City In cities
ListBox1.Items.Add(place.Name)
Next
End Sub
End Class
Last edited by dday9; May 14th, 2013 at 04:31 PM.
-
May 14th, 2013, 03:19 PM
#4
Thread Starter
Addicted Member
Re: [RESOLVED] Sorting a multidimensional array
Thanks for the suggestion dday9, however, I think you missed the part where I said:
"The cities are stored in a multidimensional array from a file"
and that my example was
"using only 3 of the thousands of cities that are stored in the array"
From what I can tell, If I used your example, I would have to type out the data/code for each of those thousands of cities and the app size would also be over-sized. I'm sure if the list was smaller and more manageable that your solution would likely be a more efficient way to do this but I don't think it is for what I'm trying to achieve.
-
May 14th, 2013, 03:24 PM
#5
Re: [RESOLVED] Sorting a multidimensional array
I have no idea what you mean by this:-
I would have to type out the data/code for each of those thousands of cities and the app size would also be over-sized.
But, a two-dimensional array is not suitable for that kind of data. dday9's example is the right way.
-
May 14th, 2013, 03:42 PM
#6
Re: [RESOLVED] Sorting a multidimensional array
 Originally Posted by SavedByGrace
I would have to type out the data/code for each of those thousands of cities and the app size would also be over-sized.
No, not really. The way you'd load the list would be different, yes, but that's all. Here is how I'd load the list if you where using a comma delimited text file:
Code:
Option Strict On
Option Explicit On
Public Class Form1
Private cities As New List(Of City)
Private Sub load_cities(ByVal file As String, ByVal delimiter As String)
'New stream reader
Dim sr As New IO.StreamReader(file)
'Returns an array of the lines
Dim str() As String = sr.ReadToEnd.Split({Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries)
'Loop through all items
For Each itm As String In str
'Get each item that's sepearted by the delimiter(in our case a comma)
Dim newarr() As String = itm.Split({delimiter}, StringSplitOptions.None)
'Declare a new city
Dim newcity As New City
'Set the city's properties
With newcity
.Name = newarr(0)
.State = newarr(1)
.Population = CInt(newarr(2))
End With
'Add it to the list
cities.Add(newcity)
Next
End Sub
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Call load_cities("c:/cities.txt", ",")
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
'Ascending
'Clear the listbox
ListBox1.Items.Clear()
'Sort by population
cities = cities.OrderBy(Function(x) x.Population).ToList
'Display results in listbox
For Each place As City In cities
ListBox1.Items.Add(place.Name & " : " & place.Population)
Next
End Sub
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
'Descending
'Clear the listbox
ListBox1.Items.Clear()
'Sort by population
cities = cities.OrderByDescending(Function(x) x.Population).ToList
'Display results in listbox
For Each place As City In cities
ListBox1.Items.Add(place.Name & " : " & place.Population)
Next
End Sub
End Class
Edit - That's assuming you have your format in a: city,state,population where the population can be converted to an integer using CInt. If the data is in a different format, setting newcity's properties would be a bit different(ie - .Name = newarr(2))
Last edited by dday9; May 14th, 2013 at 04:31 PM.
-
May 14th, 2013, 03:52 PM
#7
Thread Starter
Addicted Member
Re: [RESOLVED] Sorting a multidimensional array
 Originally Posted by Niya
I have no idea what you mean by this:-
But, a two-dimensional array is not suitable for that kind of data. dday9's example is the right way.
What I am trying to say is from the way his example code is structured (mainly the list part - see snipet of his code below), I would have to type 5 lines of code for each city with it's state and population added to the list and since the current file of cities that I am using contains about 5,000 or so cities + states + Populations, to form the list, it would take about 25,000 lines of code to take those cities & data and place them into the code/list that he is suggesting , that doesn't include the declarations of new cities.
A snipet of some of his code that forms the list which I am referring to:
Private cities As New List(Of City)
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
'Declare 3 new cities
Dim pheonix, ny, la As New City
'Set their properties
With pheonix
.Name = "Pheonix"
.State = "Arizona"
.Population = 1469471
End With
-
May 14th, 2013, 03:53 PM
#8
Re: [RESOLVED] Sorting a multidimensional array
 Originally Posted by SavedByGrace
What I am trying to say is from the way his example code is structured (mainly the list part - see snipet of his code below), I would have to type 5 lines of code for each city with it's state and population added to the list and since the current file of cities that I am using contains about 5,000 or so cities + states + Populations, to form the list, it would take about 25,000 lines of code to take those cities & data and place them into the code/list that he is suggesting , that doesn't include the declarations of new cities.
A snipet of some of his code that forms the list which I am referring to:
Take a look at my post #6 on loading the data differently.
-
May 14th, 2013, 03:54 PM
#9
Thread Starter
Addicted Member
Re: [RESOLVED] Sorting a multidimensional array
 Originally Posted by dday9
No, not really. The way you'd load the list would be different, yes, but that's all. Here is how I'd load the list if you where using a comma delimited text file:
Cool, thanks for the example of how to work with my large list and suggestion.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|