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. C#
  4. How to sort Custom class which is of type array as per one of its value

How to sort Custom class which is of type array as per one of its value

Scheduled Pinned Locked Moved C#
data-structurestutorial
7 Posts 3 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.
  • M Offline
    M Offline
    miniThomas
    wrote on last edited by
    #1

    Hi, I am having a custom class which is of type array. one of its elements is date. I want all elements of this class to be be sorted on basis of date. e.g Class Payment { date; amount; order; } Another calss { Payment[] pay=new Payment[4]; for(int i=0;i<3;i++) { pay[i]=new Payment(); pay[i].date="Monday"; pay[i].amount=5; pay[i].order=abc; } //sort pay as per date that is populated in pay say pay[0].date=Monday; pay[1].date=Wednesday; pay[2].date=Tuesday; I want the entire row of pay to be sorted on basis of date. } Thanks, Mini

    Best Regards, Mini Thomas

    L M 2 Replies Last reply
    0
    • M miniThomas

      Hi, I am having a custom class which is of type array. one of its elements is date. I want all elements of this class to be be sorted on basis of date. e.g Class Payment { date; amount; order; } Another calss { Payment[] pay=new Payment[4]; for(int i=0;i<3;i++) { pay[i]=new Payment(); pay[i].date="Monday"; pay[i].amount=5; pay[i].order=abc; } //sort pay as per date that is populated in pay say pay[0].date=Monday; pay[1].date=Wednesday; pay[2].date=Tuesday; I want the entire row of pay to be sorted on basis of date. } Thanks, Mini

      Best Regards, Mini Thomas

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      Hi, this[^] explains how to sort all kinds of collections. :)

      Luc Pattyn


      I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


      Local announcement (Antwerp region): Lange Wapper? Neen!


      1 Reply Last reply
      0
      • M miniThomas

        Hi, I am having a custom class which is of type array. one of its elements is date. I want all elements of this class to be be sorted on basis of date. e.g Class Payment { date; amount; order; } Another calss { Payment[] pay=new Payment[4]; for(int i=0;i<3;i++) { pay[i]=new Payment(); pay[i].date="Monday"; pay[i].amount=5; pay[i].order=abc; } //sort pay as per date that is populated in pay say pay[0].date=Monday; pay[1].date=Wednesday; pay[2].date=Tuesday; I want the entire row of pay to be sorted on basis of date. } Thanks, Mini

        Best Regards, Mini Thomas

        M Offline
        M Offline
        Mirko1980
        wrote on last edited by
        #3

        Populate a list with the array content. Create a custom Comparer that compares two payment objects based on date. Use the Sort method to sort the elements of the list. If you need so, convert back the list to an array.

        L 1 Reply Last reply
        0
        • M Mirko1980

          Populate a list with the array content. Create a custom Comparer that compares two payment objects based on date. Use the Sort method to sort the elements of the list. If you need so, convert back the list to an array.

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          Mirko1980 wrote:

          Populate a list with the array content.

          Why? don't you like public static void Sort(Array array, IComparer comparer)? :doh:

          Luc Pattyn


          I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


          Local announcement (Antwerp region): Lange Wapper? Neen!


          M 1 Reply Last reply
          0
          • L Luc Pattyn

            Mirko1980 wrote:

            Populate a list with the array content.

            Why? don't you like public static void Sort(Array array, IComparer comparer)? :doh:

            Luc Pattyn


            I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


            Local announcement (Antwerp region): Lange Wapper? Neen!


            M Offline
            M Offline
            Mirko1980
            wrote on last edited by
            #5

            Nothing, only I didn't think about it :-O

            M 1 Reply Last reply
            0
            • M Mirko1980

              Nothing, only I didn't think about it :-O

              M Offline
              M Offline
              miniThomas
              wrote on last edited by
              #6

              Hi Guys, I have alerady tried with IComparable but it doesn't return sorted array, maybe I am implementing in the wrong way. Can you guys help me out with the exact implementation? This payment Array is being accessed in another class say PaymentHistory and I wanna sort as per date the Payment array in PaymentHistory class.

              Best Regards, Mini Thomas

              M 1 Reply Last reply
              0
              • M miniThomas

                Hi Guys, I have alerady tried with IComparable but it doesn't return sorted array, maybe I am implementing in the wrong way. Can you guys help me out with the exact implementation? This payment Array is being accessed in another class say PaymentHistory and I wanna sort as per date the Payment array in PaymentHistory class.

                Best Regards, Mini Thomas

                M Offline
                M Offline
                Mirko1980
                wrote on last edited by
                #7

                What have you tried? IComparable alone does not sort anything. Are you using Array.Sort? Or a List? Post the code you are using.

                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