array headache

have had this huge problem understanding how to store data in array,like a simple programme of storing name slary id etc in a 3d array,how would i go about it if i wanted the output sorted in ascending or descending order.all help will b highly appreciated.thanx

Comments

  • : have had this huge problem understanding how to store data in array,like a simple programme of storing name slary id etc in a 3d array,how would i go about it if i wanted the output sorted in ascending or descending order.all help will b highly appreciated.thanx
    :


    Hi ambu

    In my experience, if I'm going to be working with anything other than very primitive arrays, I find it is always worth going to the extra trouble of using a Collection. In your case, you can create a fairly simple People class, with Name, Salary, ID, etc properties. You can then build your own PeopleCollection and ths will give you total and minute control over every detail, plus you can create any kind of Sort type (well, almost!) that you want. Adding and removing single items or groups of items becomes very easy.

    If you are interested in going down this route, post a follow-up and I will give you some code examples and also links to excellent and easy to follow articles on how to do this.

    Ged

  • [b][red]This message was edited by Relman at 2004-11-2 7:35:41[/red][/b][hr]
    [red]Just fix a mistake[/red]

    : have had this huge problem understanding how to store data in array,like a simple programme of storing name slary id etc in a 3d array,how would i go about it if i wanted the output sorted in ascending or descending order.all help will b highly appreciated.thanx
    :

    I'm a bit confused as to why the array is 3d. You say that you want to store Id, Name, Salary. Are they all the same type? You don't need a 3d array to store them, you can make an array of a "Structure" and write your own code to sort it. The sorting method that is built-in to Array objects only works for 1 dimentional arrays.

    I looked around in the help system and found a way to sort an array of Structures. Lets say we have a structure:
    [code] Structure Employee
    Dim Name As String
    Dim Id As Integer
    Dim Salary As Integer
    End Structure[/code]

    And we created an array of them:
    [code]Dim Employees(10) As Employee[/code]

    How do we sort them by id?

    Well, This is what I found:
    The way that the built-in sorter works is, using a comparer class that implements the IComparer Interface, it compares each element in the array with every other one and checks which one is bigger. (Actually, it doesn't do exactly this, but the main thing is that it needs to be able to compare). What we need to do is create a class that implements the IComparer interface so that the sort method knows which element should precede another. To do this, we have to override the compare function of the IComparer interface. The compare function takes two objects as its parameters and returns an integer as its result. Lets say that X and y are the two parameters. Then:
    if X < Y then return a negative number
    if X = Y then return zero
    if X > Y then return a positive number
    So what we do is create a class that implements the IComparer interface and has a method that can compare two Employees and tell us which is bigger.

    The code is something like this:
    [code]
    Class EmployeeComparer [green]'Create a class[/green]
    Implements IComparer [green]'Implement the IComparer Interface[/green]
    Overloads Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements IComparer.Compare
    If CType(x, Employee).Id < CType(y, Employee).Id Then Return -1
    If CType(x, Employee).Id > CType(y, Employee).Id Then Return 1
    Return 0 'Otherwise, they are =. Return 0
    [green]'BTW, if they are = then you can make it sort alphabetically
    'and so on and so forth.
    'Alseo, this method can be simplified to one line:
    'Return CType(x, Employee).Id - CType(y, Employee).Id[/green]
    End Function
    End Class[/code]
    Now with this class, the Sort procedure knows which Employee to give precedence (The one with the bigger ID). Now we can do your sort:
    [code]Dim i as Integer
    Randomize
    For i = 0 to 10
    Employees(i).Id = Rnd()*100 + 1 [green]'Generate a random number between 1 and 100 to test our comparer class[/green]
    Employees(i).Name = Chr(Asc("a") + i - 1) [green]'Get the letters from a to k[/green]
    Next i
    [green]'Now that we have generated some random Employees, lets show them[/green]
    For i = 0 To 10
    MsgBox(Employees(i).Id & " " & Employees(i).Name)
    Next
    [green]'Create an instance of our comparer[/green]
    Dim Comp as New EmployeeComparer
    [green]'Now lets use our interface to sort[/green]
    Employees.Sort(Employees, Comp)
    [green]'Now lets show them again and see if they have been sorted[/green]
    For i = 0 To 10
    MsgBox(Employees(i).Id & " " & Employees(i).Name)
    Next
    [/code]

    To sort descending, just use "Employees.Reverse" after sorting it.

    I hope that explanation wasn't confusing and that that I provided you with can solve your problem.

    As always, I hope this helps,

    Relman

    BTW, I also gained by looking into this. Until 10 minutes ago, I didn't know how easy it was to sort an array of a structure. "More than your teach your students, your students teach you" (Pirkei Avos AKA Ethics of our Fathers)
  • thanks alot you can be sure i will be sending for more help soon.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories