Sorting in C#

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bravo659
    New Member
    • Nov 2009
    • 4

    Sorting in C#

    How do you sort in C# programmaticall y.
    Meaning not to use the properties window to set sort to true.
    Meaning not to use Array.Sort feature either.
    This is actually a sort array coded programmaticall y at runtime.

    Thanks i appreciate for your help.
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    You want to create your own sorting implementation? There's going to be thosaunds of websites that detail the algorithms. Try googling for some.

    Comment

    • bravo659
      New Member
      • Nov 2009
      • 4

      #3
      I did and got this site. This site actually stated that I can get help in programming. I just needed some help. So here I am asking. Can anyone in this forum help me in sorting in C#.

      Thanks.

      Comment

      • cloud255
        Recognized Expert Contributor
        • Jun 2008
        • 427

        #4
        Sorting is based on algorithms, its not really language specific. Several of the .NET collections have sorting methods already exposed. For us to help you with a sorting algorithm you need to be more specific. What datastructure are you trying to sort? Is there any specific sorting technique you would like to use?

        Have a look at Insertion sort, Selection sort, Bubble sort and Quick sort These are all fairly well known and easy to implement algorithms.

        Comment

        • Plater
          Recognized Expert Expert
          • Apr 2007
          • 7872

          #5
          I didn't mean for it to sound like I was giving you the "runaround" .
          Your question wasn't very clear. You never actually said "I am trying to implement my own sorting functions, but am not sure how to start"
          Cloud went a step further then me and did some leg work for you, finding links to common sorting algorithms, which is what I was trying to get at by suggesting to google it.
          We will not write your sorting function for you, but we can help you if you're stuck in your implementation.

          Comment

          • bravo659
            New Member
            • Nov 2009
            • 4

            #6
            Hello Cloud and Plater,

            My bad I forgot to say that.
            I am trying to have the contents of a textbox displayed in the listbox sorted by lastname and alphabetically. I know about the feature in properties but they want it to be done programmaticall y. I am not sure how to go about it. But I know its an array that I need to sort. The feature in the .Net Framework is Array.Sort. Now in order to be valid it has to be done programmaticall y.

            I also previously posted my entire code for the AddressBook array. I cannot get it to work accordingly.

            Can you help me.

            Comment

            • Plater
              Recognized Expert Expert
              • Apr 2007
              • 7872

              #7
              Well i would consider Array.Sort() to be programmaticall y, chances are pretty good that's what was meant. Just not having to sort it by hand.

              If you want to use array.sort(), you will probably have to deal with either ICompare interface or the IComparable interface (depending on how you want to tackle it) The basic idea is to provide a function that takes in two different objects of the same type and you return either a -1,0,1 to say if object A comes before, is the same, or after object b.
              You would have access to all of A and Bs values, so you can compare by last name then sort by first name.

              Comment

              • Curtis Rutland
                Recognized Expert Specialist
                • Apr 2008
                • 3264

                #8
                Is this a schoolwork assignment? If so, read up on the links that cloud provided, and implement one of those algorithms.

                If not, then the Array.Sort() method is perfect. If you need custom sorting, I prefer to use the IComparable interface.

                Comment

                • bravo659
                  New Member
                  • Nov 2009
                  • 4

                  #9
                  Hello inSertAlias,

                  Yes I agree I wish I could use the Array.Sort is much better and easier.
                  I have to study a little more on the sort you mentioned that Cloud pointed out for me. Ok thanks all.

                  I'll try my best to do this final assignment. Been working on it for a week and is due next Tuesday but thanks all and I understand. Thanks

                  Regards,

                  Desi Bravo

                  Comment

                  Working...