locked
Sorting in Visual Basic .Net 2010

    Question

  • I am using structures in my programming and then sort the structure according to a value in the structure using ICOMPARER. I am using visual basic .Net 2010.

    My question is how Microsoft programmers do programmed the “SORT” command? Is there any documentation (references) for this? Is it the same for all types of “SORT”s in visual basic?

    This is a simple example for what I want.

        Dim MyArray(6) As Integer

            MyArray(0) = 1

            MyArray(1) = 45

            MyArray(2) = 45

           ' Some Code.....

            '.........

            '..........

            MyArray(3) = 1

            MyArray(4) = 10

            ' Some Code.....

            '.........

            '..........

            MyArray(5) = 1

            MyArray(6) = 57

            Array.Sort(MyArray)

     

    Basically, the SORT command will sort this array as: (1     1              1              10            45            45            57)

    The question is how do number 1 is sorted? Is it bringing to the end the first one or keeps the old one in the same index?

    In my original example (before sorting), MyArray(0) = 1 and after sorting MyArray(0) = 1. My question, is this the same original 1 or this another 1 (the newest one added to the array) moved to that position. In case the MyArray(0) = 1 after sorting should be MyArray(5) = 1 before sorting.

     

    Regards


    Cheers! Aharoun Baalan
    Wednesday, October 27, 2010 2:43 PM

Answers

  • Hi again Aharoun

    The answer i gave was of a basic array.sort. The Array indexes will be in asscending order if you have more than one entry of the same value. Have a look at this

    http://msdn.microsoft.com/en-us/library/8hz451zc(v=VS.90).aspx

    it is in VB 2008 but i will work with VB2010.

    Some documentation to do with Array Sorting. Read through it and it shows examples, if you follow the links at the bottom of the page.

    Hope this helps

    Regards

    ______________________________________________________

    Zeldacat

    • Marked as answer by Haroun Baalan Sunday, October 31, 2010 6:13 PM
    Thursday, October 28, 2010 12:11 AM

All replies

  • Hi  Aharoun

    I'm trying to figure out what you are trying to say here. Allowing for your language difficulty, you have sorted your array in asscending order(1,2,3 etc). If i have understood you correctly, your array indexes would be

    myArray(0) = 1

    myArray(3) = 1

    myArray(5) = 1

    myArray(4) = 10

    myArray(1) = 45

    myArray(2) = 45

    myArray(6) = 57

    Hope this helps

    Regards 

    ______________________________________________

    Zeldacat

     

    Wednesday, October 27, 2010 9:08 PM

  • Hi  Aharoun

    I'm trying to figure out what you are trying to say here. Allowing for your language difficulty, you have sorted your array in asscending order(1,2,3 etc). If i have understood you correctly, your array indexes would be

    myArray(0) = 1

    myArray(3) = 1

    myArray(5) = 1

    myArray(4) = 10

    myArray(1) = 45

    myArray(2) = 45

    myArray(6) = 57

    Hope this helps

    Regards 

    ______________________________________________

    Zeldacat

     

    HI Zeldacat

    Thank you for your reply.

    From your answer, it means that SORT keeps the first minimum number and put the similar behind it.

    Do you have any references or documentation for this answer? 

    I need this because I am doing a research and proving this theory is very important.

     

    Regards

    Aharoun

     

     


    Cheers! Aharoun Baalan
    Wednesday, October 27, 2010 10:13 PM
  • Hi again Aharoun

    The answer i gave was of a basic array.sort. The Array indexes will be in asscending order if you have more than one entry of the same value. Have a look at this

    http://msdn.microsoft.com/en-us/library/8hz451zc(v=VS.90).aspx

    it is in VB 2008 but i will work with VB2010.

    Some documentation to do with Array Sorting. Read through it and it shows examples, if you follow the links at the bottom of the page.

    Hope this helps

    Regards

    ______________________________________________________

    Zeldacat

    • Marked as answer by Haroun Baalan Sunday, October 31, 2010 6:13 PM
    Thursday, October 28, 2010 12:11 AM
  • Thanks again for your reply.

    I just discovered that Array.Sort() is a quick and unstable sort method. It does not preserve the position of the sorted numbers. See Array.Sort()

    On the other hand, there are stable sorting techniques. You can read the article in MSDN.

     

    Now, I have to find a method which allows me to sort my Structure with preserving the order of similar sorted numbers.

    Regards

    Aharoun

     

     


    Cheers! Aharoun Baalan
    Thursday, October 28, 2010 8:46 AM
  • Hi Aharoun

    Sorry the array.sort method wasn't for you.

    Have a look at this article for Redim Preserve.

    http://msdn.microsoft.com/en-us/library/w8k3cys2(v=VS.100).aspx

    You said 

    Now, I have to find a method which allows me to sort my Structure with preserving the order of similar sorted numbers.

    So maybe this will put you on the right track.

    Regards

    Zeldacat

    Thursday, October 28, 2010 9:44 AM
  • OK. I agree with you. Please find my problem below:

     

    Public Structure ToSolve

            Dim Connections(,) As Integer

            Dim Discharge(,) As Double

            Dim Value As Double

           

            Sub New(ByVal ASol As Integer)

                ReDim Con(ASol, ASol)

                ReDim Discharge(ASolution, ASol)

           End Sub

        End Structure

     

    Public Populate(1) As ToSolve

     

    Public Class ValueComparer

            Implements IComparer(Of ToSolve)

            Public Function Compare(ByVal x As ToSolve, ByVal y As ToSolve) As _

        Integer Implements System.Collections.Generic.IComparer(Of ToSolve).Compare

                If x.Value > y.Value Then

                    Return 1

                Else

                    If x.Value < y.Value Then

                        Return -1

                    Else

                        Return 0

                    End If

               End If

            End Function

        End Class

     

    In the body of the program, I redim ‘populate’ and do some calculations. Then I want to sort the structure.

                Array.Sort(Populate, New ValueComparer)

     

     

    Using all these methods, how can I preserve the positions of similar number in the list? i.e. I want to bring the newest ‘Similar’ value to the top of the list.

     

    Many thanks!

     


    Cheers! Aharoun Baalan
    • Edited by Haroun Baalan Thursday, October 28, 2010 2:39 PM Typo
    Thursday, October 28, 2010 2:38 PM
  • Hi Ahoaroun

    I don't know if this will help, but. This comes from an experianced programmer

    'im looking at Array now to see if there are any clues, but my guess is that the algorithm is stable, so that the order of items of the same value doesn't change

    Array.Sort says it uses the quick sort algorithm but it is unstable, because of that you can't tell what order items with the same value will come out in

    Which is exactly what you said earlier.

    So i looked up other stuff and  using redim preserve would have ended up like a lot more work,

    Another idea  would suggest using something like arraylist or some other container other than array for this. It could give you better scope on what you might want to do. so check this out

    http://msdn.microsoft.com/en-us/library/system.collections.arraylist(v=VS.90).aspx

    It is for VS 2008 but it works in VS 2010 fine.

    I hope this is a little more help...

     


    Regards ____________________________________________________ Zeldacat
    Friday, October 29, 2010 12:27 AM