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 arraylist ?

How to sort arraylist ?

Scheduled Pinned Locked Moved C#
tutorialalgorithmsquestion
11 Posts 5 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.
  • S Saikek

    Hello! I have a ArrayList, that contains class (for example): class A { int a int b public A() { a=0; b=0; } public A(int Na, int Nb) { a=Na; b=Nb; } } How to sort this elements in the arraylist ? The sorting field is A.

    One nation - underground

    R Offline
    R Offline
    rah_sin
    wrote on last edited by
    #2

    u can try arraylist's Sort method

    rahul

    1 Reply Last reply
    0
    • S Saikek

      Hello! I have a ArrayList, that contains class (for example): class A { int a int b public A() { a=0; b=0; } public A(int Na, int Nb) { a=Na; b=Nb; } } How to sort this elements in the arraylist ? The sorting field is A.

      One nation - underground

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #3

      If you're using .NET 2.0, use a List instead and provide a sorting function. I can't recall, but I thought you can do this in 1.1 and 1.0 as well, probably just with lots of casting of objects.

      Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

      D 1 Reply Last reply
      0
      • S Saikek

        Hello! I have a ArrayList, that contains class (for example): class A { int a int b public A() { a=0; b=0; } public A(int Na, int Nb) { a=Na; b=Nb; } } How to sort this elements in the arraylist ? The sorting field is A.

        One nation - underground

        M Offline
        M Offline
        Martin 0
        wrote on last edited by
        #4

        Hello,

        Saikek wrote:

        The sorting field is A.

        I don't understand this! Don't you mean you whant to sort your Collection of "A"-class instances, with the value of the "A"-class member "a" (which is an int)? Martin

        1 Reply Last reply
        0
        • S Saikek

          Hello! I have a ArrayList, that contains class (for example): class A { int a int b public A() { a=0; b=0; } public A(int Na, int Nb) { a=Na; b=Nb; } } How to sort this elements in the arraylist ? The sorting field is A.

          One nation - underground

          D Offline
          D Offline
          DavidNohejl
          wrote on last edited by
          #5

          As others said, if you are using .NET 2.0 use generic collection (List<A>). As for sorting, you probably mean sorting by field int 'a'? As you dont expose anything (a,b is private) you could implement IComparable. Or if you can expose sorting field, custom implementation of IComparer that knows how to sort class A (e.g. A.SortByFieldAComparer). Look up appropriate interfaces in MSDN or google.


          "Throughout human history, we have been dependent on machines to survive. Fate, it seems, is not without a sense of irony. " - Morpheus "Real men use mspaint for writing code and notepad for designing graphics." - Anna-Jayne Metcalfe

          M 1 Reply Last reply
          0
          • C Christian Graus

            If you're using .NET 2.0, use a List instead and provide a sorting function. I can't recall, but I thought you can do this in 1.1 and 1.0 as well, probably just with lots of casting of objects.

            Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

            D Offline
            D Offline
            DavidNohejl
            wrote on last edited by
            #6

            Christian Graus wrote:

            I thought you can do this in 1.1 and 1.0 as well, probably just with lots of casting of objects.

            Yup, for sorting operation there must be operators <, >,= defined (or some of them at least), which System.object does not have. :)


            "Throughout human history, we have been dependent on machines to survive. Fate, it seems, is not without a sense of irony. " - Morpheus "Real men use mspaint for writing code and notepad for designing graphics." - Anna-Jayne Metcalfe

            1 Reply Last reply
            0
            • D DavidNohejl

              As others said, if you are using .NET 2.0 use generic collection (List<A>). As for sorting, you probably mean sorting by field int 'a'? As you dont expose anything (a,b is private) you could implement IComparable. Or if you can expose sorting field, custom implementation of IComparer that knows how to sort class A (e.g. A.SortByFieldAComparer). Look up appropriate interfaces in MSDN or google.


              "Throughout human history, we have been dependent on machines to survive. Fate, it seems, is not without a sense of irony. " - Morpheus "Real men use mspaint for writing code and notepad for designing graphics." - Anna-Jayne Metcalfe

              M Offline
              M Offline
              Martin 0
              wrote on last edited by
              #7

              Hello, Just because I'm curious: Do you know of a equivalent Comparer class to System.Collection.CaseInsensitiveComparer, which comparse int values? Or is the solution to use "ToString" for comparison.

              dnh wrote:

              As for sorting, you probably mean sorting by field int 'a'?

              I'm happy that it's not only me, who thinks that the question is not valid. All the best, Martin

              D 1 Reply Last reply
              0
              • M Martin 0

                Hello, Just because I'm curious: Do you know of a equivalent Comparer class to System.Collection.CaseInsensitiveComparer, which comparse int values? Or is the solution to use "ToString" for comparison.

                dnh wrote:

                As for sorting, you probably mean sorting by field int 'a'?

                I'm happy that it's not only me, who thinks that the question is not valid. All the best, Martin

                D Offline
                D Offline
                DavidNohejl
                wrote on last edited by
                #8

                Hmm, there is only one way how to compare integers (that makes sense) as far as I know. System.Int32 implements IComparable, so no need for comparer.


                "Throughout human history, we have been dependent on machines to survive. Fate, it seems, is not without a sense of irony. " - Morpheus "Real men use mspaint for writing code and notepad for designing graphics." - Anna-Jayne Metcalfe

                M 1 Reply Last reply
                0
                • D DavidNohejl

                  Hmm, there is only one way how to compare integers (that makes sense) as far as I know. System.Int32 implements IComparable, so no need for comparer.


                  "Throughout human history, we have been dependent on machines to survive. Fate, it seems, is not without a sense of irony. " - Morpheus "Real men use mspaint for writing code and notepad for designing graphics." - Anna-Jayne Metcalfe

                  M Offline
                  M Offline
                  Martin 0
                  wrote on last edited by
                  #9

                  Hello dnh, 2 minutes after I posted this yesterday, I hoped you would ignore it. :rolleyes: So the .Net 1.1 solution for me would be, use the Sort method of the ArrayList class and give a user specific IComparer instance as parameter. Userspecific IComparer instance would be something like this:

                  public class ComparerForIntMemberaInClassA : IComparer
                  {
                  int System.Collections.IComparer.Compare( object x, object y )
                  {
                  A firstA = x as A;
                  A secondA = y as A;
                  return firstA.a.CompareTo(secondA.a);
                  }
                  }

                  What's your feeling about it? Thank you for you interest! All the best, Martin

                  D 1 Reply Last reply
                  0
                  • M Martin 0

                    Hello dnh, 2 minutes after I posted this yesterday, I hoped you would ignore it. :rolleyes: So the .Net 1.1 solution for me would be, use the Sort method of the ArrayList class and give a user specific IComparer instance as parameter. Userspecific IComparer instance would be something like this:

                    public class ComparerForIntMemberaInClassA : IComparer
                    {
                    int System.Collections.IComparer.Compare( object x, object y )
                    {
                    A firstA = x as A;
                    A secondA = y as A;
                    return firstA.a.CompareTo(secondA.a);
                    }
                    }

                    What's your feeling about it? Thank you for you interest! All the best, Martin

                    D Offline
                    D Offline
                    DavidNohejl
                    wrote on last edited by
                    #10

                    yes, that looks fine. Other solution would be implement IComparable in class A; I don't know the guidelines but to me it feels like for "default" sorting I would implement IComparable in class I want to sort (if it is class I own - if it is e.g .NET framework class then I can't do that) and non standard comparations (e.g case insensitive) would go into custom comparers. If you are sorting by private members, nested class should solve access problem.


                    "Throughout human history, we have been dependent on machines to survive. Fate, it seems, is not without a sense of irony. " - Morpheus "Real men use mspaint for writing code and notepad for designing graphics." - Anna-Jayne Metcalfe

                    M 1 Reply Last reply
                    0
                    • D DavidNohejl

                      yes, that looks fine. Other solution would be implement IComparable in class A; I don't know the guidelines but to me it feels like for "default" sorting I would implement IComparable in class I want to sort (if it is class I own - if it is e.g .NET framework class then I can't do that) and non standard comparations (e.g case insensitive) would go into custom comparers. If you are sorting by private members, nested class should solve access problem.


                      "Throughout human history, we have been dependent on machines to survive. Fate, it seems, is not without a sense of irony. " - Morpheus "Real men use mspaint for writing code and notepad for designing graphics." - Anna-Jayne Metcalfe

                      M Offline
                      M Offline
                      Martin 0
                      wrote on last edited by
                      #11

                      Thanks for your statement! Have a nice day! All the best, Martin

                      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