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. sorting an ArrayList with different attributes

sorting an ArrayList with different attributes

Scheduled Pinned Locked Moved C#
questionalgorithmshelp
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.
  • M Mostafa Siraj

    hi all since i can't describe my problem well, plz check this code, in this code i can sort the list for the x variable -this is fine and working- but wat if i want also to be able to sort the list with y variable as well. i can only use IComparable.Compare with x, i want also to be able to sort the list for y variable how can i do that???? thanks in advance how can i do that using System; using System.Collections; using System.Collections.Generic; public class SamplesArrayList { public class Test : IComparable { public int x; public int y; //this is the variable which i want to sort the list with it also public Test(int x) { this.x = x; } int IComparable.CompareTo(object x) { Test x1 = (Test)x; return (this.x.CompareTo(x1.x)); } } public static void Main() { ArrayList test = new ArrayList(); Test l1 = new Test(7); Test l2 = new Test(3); Test l3 = new Test(9); Test l4 = new Test(2); Test l5 = new Test(4); test.Add(l1); test.Add(l2); test.Add(l3); test.Add(l4); test.Add(l5); test.Sort(); } }

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

    yosif4444 wrote:

    int IComparable.CompareTo(object x) { Test x1 = (Test)x; return (this.x.CompareTo(x1.x)); }

    Your compare to can do whatever you like. You return an int, which specifies how far apart two items are, and in what direction. If you want to sort for y within x, then this would work: if (x == x1.x) { return y - x1.y); } else { return x - x1.x; } Or, pass on to the int compareTo, if you want.

    Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

    M S 2 Replies Last reply
    0
    • C Christian Graus

      yosif4444 wrote:

      int IComparable.CompareTo(object x) { Test x1 = (Test)x; return (this.x.CompareTo(x1.x)); }

      Your compare to can do whatever you like. You return an int, which specifies how far apart two items are, and in what direction. If you want to sort for y within x, then this would work: if (x == x1.x) { return y - x1.y); } else { return x - x1.x; } Or, pass on to the int compareTo, if you want.

      Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

      M Offline
      M Offline
      Mostafa Siraj
      wrote on last edited by
      #3

      i think i described my problem wrong i want to sort the list with either x or y not with both of them say object1 x = 5, y = 10 object2 x = 3, y = 7 object3 x = 7, y = 4 Test.Sort() //for x output object2 object1 object3 or Test.Sort() //for y output object3 object2 object1 i hope u got wat i mean thanks

      L C 2 Replies Last reply
      0
      • M Mostafa Siraj

        i think i described my problem wrong i want to sort the list with either x or y not with both of them say object1 x = 5, y = 10 object2 x = 3, y = 7 object3 x = 7, y = 4 Test.Sort() //for x output object2 object1 object3 or Test.Sort() //for y output object3 object2 object1 i hope u got wat i mean thanks

        L Offline
        L Offline
        led mike
        wrote on last edited by
        #4

        yosif4444 wrote:

        i want to sort the list with either x or y not with both of them

        So what part of "do whatever you like" don't you understand? :rolleyes:

        Christian Graus wrote:

        Your compare to can do whatever you like

        led mike

        M 1 Reply Last reply
        0
        • L led mike

          yosif4444 wrote:

          i want to sort the list with either x or y not with both of them

          So what part of "do whatever you like" don't you understand? :rolleyes:

          Christian Graus wrote:

          Your compare to can do whatever you like

          led mike

          M Offline
          M Offline
          Mostafa Siraj
          wrote on last edited by
          #5

          I know that i can "do whatever i want" but only in one compare function i want to write many compare functions depending on the variable i want

          1 Reply Last reply
          0
          • M Mostafa Siraj

            i think i described my problem wrong i want to sort the list with either x or y not with both of them say object1 x = 5, y = 10 object2 x = 3, y = 7 object3 x = 7, y = 4 Test.Sort() //for x output object2 object1 object3 or Test.Sort() //for y output object3 object2 object1 i hope u got wat i mean thanks

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

            OK - I get it. Create an enum for the different sort orders, then use an instance of that to decide what to do in the compare function.

            Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

            M 1 Reply Last reply
            0
            • C Christian Graus

              OK - I get it. Create an enum for the different sort orders, then use an instance of that to decide what to do in the compare function.

              Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

              M Offline
              M Offline
              Mostafa Siraj
              wrote on last edited by
              #7

              Yeah you got my point but can u plz help me with code example, coz i don't know how to implement wat u said thanks in advance

              C 1 Reply Last reply
              0
              • M Mostafa Siraj

                Yeah you got my point but can u plz help me with code example, coz i don't know how to implement wat u said thanks in advance

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

                OK, it's pretty simple public class SamplesArrayList { public class Test : IComparable { public enum SortBy { SortByX, SortByY }; private SortBy _sortOrder; public SortBy { get { return _sortOrder; } set { _sortOrder = value; } } public int x; public int y; //this is the variable which i want to sort the list with it also public Test(int x) { this.x = x; } int IComparable.CompareTo(object x) { Test x1 = (Test)x; switch(SortOrder) { int result; case SortBy.SortByX: result = (this.x.CompareTo(x1.x)); break; case SortBy.SortByY: result = (this.y.CompareTo(x1.y)); break; default: throw new ArgumentException("The SortBy enum has been added to, but the CompareTo method has not been updated"); break; } return result; } }

                Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

                1 Reply Last reply
                0
                • M Mostafa Siraj

                  hi all since i can't describe my problem well, plz check this code, in this code i can sort the list for the x variable -this is fine and working- but wat if i want also to be able to sort the list with y variable as well. i can only use IComparable.Compare with x, i want also to be able to sort the list for y variable how can i do that???? thanks in advance how can i do that using System; using System.Collections; using System.Collections.Generic; public class SamplesArrayList { public class Test : IComparable { public int x; public int y; //this is the variable which i want to sort the list with it also public Test(int x) { this.x = x; } int IComparable.CompareTo(object x) { Test x1 = (Test)x; return (this.x.CompareTo(x1.x)); } } public static void Main() { ArrayList test = new ArrayList(); Test l1 = new Test(7); Test l2 = new Test(3); Test l3 = new Test(9); Test l4 = new Test(2); Test l5 = new Test(4); test.Add(l1); test.Add(l2); test.Add(l3); test.Add(l4); test.Add(l5); test.Sort(); } }

                  R Offline
                  R Offline
                  Russell Jones
                  wrote on last edited by
                  #9

                  this might help ArrayList.Sort Method (IComparer) HTH Russ

                  1 Reply Last reply
                  0
                  • C Christian Graus

                    yosif4444 wrote:

                    int IComparable.CompareTo(object x) { Test x1 = (Test)x; return (this.x.CompareTo(x1.x)); }

                    Your compare to can do whatever you like. You return an int, which specifies how far apart two items are, and in what direction. If you want to sort for y within x, then this would work: if (x == x1.x) { return y - x1.y); } else { return x - x1.x; } Or, pass on to the int compareTo, if you want.

                    Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

                    S Offline
                    S Offline
                    sanjaybs2001
                    wrote on last edited by
                    #10

                    Try it --- using System; using System.Collections; using System.Collections.Generic; public class SamplesArrayList { static char indi; public class Test : IComparable { public int x; public int y; //this is the variable which i want to sort the list with it also public char indi; public Test(int x, int y) { this.x = x; this.y = y; } int IComparable.CompareTo(object x) { if (indi == 'X') { Test x1 = (Test)x; return (this.x.CompareTo(x1.x)); } else { Test y1 = (Test)x; return (this.y.CompareTo(y1.y)); } } } public static void Main() { ArrayList test = new ArrayList(); Test l1 = new Test(7,8); Test l2 = new Test(3,2); Test l3 = new Test(9,1); Test l4 = new Test(2,0); Test l5 = new Test(4,5); indi = 'X'; test.Add(l1); test.Add(l2); test.Add(l3); test.Add(l4); test.Add(l5); test.Sort(); foreach (Test t in test) { Test x1 = t; Console.WriteLine(x1.x+ " "+ x1.y); } Console.ReadKey(); } }

                    C 1 Reply Last reply
                    0
                    • S sanjaybs2001

                      Try it --- using System; using System.Collections; using System.Collections.Generic; public class SamplesArrayList { static char indi; public class Test : IComparable { public int x; public int y; //this is the variable which i want to sort the list with it also public char indi; public Test(int x, int y) { this.x = x; this.y = y; } int IComparable.CompareTo(object x) { if (indi == 'X') { Test x1 = (Test)x; return (this.x.CompareTo(x1.x)); } else { Test y1 = (Test)x; return (this.y.CompareTo(y1.y)); } } } public static void Main() { ArrayList test = new ArrayList(); Test l1 = new Test(7,8); Test l2 = new Test(3,2); Test l3 = new Test(9,1); Test l4 = new Test(2,0); Test l5 = new Test(4,5); indi = 'X'; test.Add(l1); test.Add(l2); test.Add(l3); test.Add(l4); test.Add(l5); test.Sort(); foreach (Test t in test) { Test x1 = t; Console.WriteLine(x1.x+ " "+ x1.y); } Console.ReadKey(); } }

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

                      using a char is obviously nasty as hell, but this is the general idea, yes.

                      Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

                      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