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 Array

Sorting an Array

Scheduled Pinned Locked Moved C#
algorithmsdata-structuresquestion
19 Posts 6 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.
  • E electriac

    I have spent a whole day trying to write a comarer and make sence out of MSDN's convoluted instructions but have not been able to get something that compiles and works.

    E Offline
    E Offline
    Ennis Ray Lynch Jr
    wrote on last edited by
    #8

    public class Foo : Comparer<foo> { private string mMyString = new string('c', 100); public override int Compare(Foo one, Foo two) { return one.mMyString.Substring(59).CompareTo(two.mMyString.Substring(59)); } } Use System.Collections.Generic.List as the list. Also, you may have to vary the code in the Compare method as I did not bother to test it.

    Need a C# Consultant? I'm available.
    Happiness in intelligent people is the rarest thing I know. -- Ernest Hemingway

    1 Reply Last reply
    0
    • E electriac

      I have spent a whole day trying to write a comarer and make sence out of MSDN's convoluted instructions but have not been able to get something that compiles and works.

      G Offline
      G Offline
      Guffa
      wrote on last edited by
      #9

      A comparer is pretty simple. It's just a class with a Compare method that works like any other Compare method (like string.Compare for example). Try this:

      class SubstringComparer : IComparer<string> {
      private int _offset, _len;
      public SubstringComparer(int offset, int len) { _offset = offset; _len = len; }
      public override int Compare(string x, string y) {
      return string.Compare(x, _offset, y, _offset, _len);
      }
      }

      Array.Sort(myArray, new SubstringComparer(59, int.MaxValue));

      modified on Saturday, December 08, 2007 2:31:24 PM

      E 1 Reply Last reply
      0
      • G Guffa

        A comparer is pretty simple. It's just a class with a Compare method that works like any other Compare method (like string.Compare for example). Try this:

        class SubstringComparer : IComparer<string> {
        private int _offset, _len;
        public SubstringComparer(int offset, int len) { _offset = offset; _len = len; }
        public override int Compare(string x, string y) {
        return string.Compare(x, _offset, y, _offset, _len);
        }
        }

        Array.Sort(myArray, new SubstringComparer(59, int.MaxValue));

        modified on Saturday, December 08, 2007 2:31:24 PM

        E Offline
        E Offline
        electriac
        wrote on last edited by
        #10

        This is my code that will not compile. class SubstringComparer : IComparer<string> { private int _offset, _len; public SubstringComparer(int offset, int len) { _offset = offset; _len = len; } public override int Compare(string x, string y) //--------------------------------------------------------- // I get no suitable method found to override Compare //--------------------------------------------------------- { return string.Compare(x, _offset, y, _offset, _len); } } private void button13_Click(object sender, EventArgs e) { ArrayList arraylist = new ArrayList(); for (int i = 0; i < listBox1.Items.Count; i++) { string temp = listBox1.Items[i].ToString(); if (temp.Length>65) { arraylist.Add(temp); } } Array.Sort(arraylist, new SubstringComparer(59, int.MaxValue)); listBox1.Items.Clear(); for (int i = 0; i < arraylist.Count; i++) listBox1.Items.Add(arraylist[i]); } } TNX for the help

        modified on Saturday, December 08, 2007 10:45:32 AM

        L G 2 Replies Last reply
        0
        • E electriac

          This is my code that will not compile. class SubstringComparer : IComparer<string> { private int _offset, _len; public SubstringComparer(int offset, int len) { _offset = offset; _len = len; } public override int Compare(string x, string y) //--------------------------------------------------------- // I get no suitable method found to override Compare //--------------------------------------------------------- { return string.Compare(x, _offset, y, _offset, _len); } } private void button13_Click(object sender, EventArgs e) { ArrayList arraylist = new ArrayList(); for (int i = 0; i < listBox1.Items.Count; i++) { string temp = listBox1.Items[i].ToString(); if (temp.Length>65) { arraylist.Add(temp); } } Array.Sort(arraylist, new SubstringComparer(59, int.MaxValue)); listBox1.Items.Clear(); for (int i = 0; i < arraylist.Count; i++) listBox1.Items.Add(arraylist[i]); } } TNX for the help

          modified on Saturday, December 08, 2007 10:45:32 AM

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

          Hi George, there are basically two ways (old and new): 1. if you want to sort an ArrayList in a special way, you need to create an object that implements IComparer (without < string> ) and knows how to compare two items. Its signature MUST be public int Compare(object obj1, object obj2) There is no override, by writing ": Icomparer" you promise to implement the formentioned method. And you need to cast the arguments to strings explicitly, as in: string str1=(string)obj1 2. Starting with .NET 2.0 you can also use generic collections. The code Guffa showed you is fine if you keep the strings in a generic list, that is a List< string>. In that case you inherit from IComparer< string> and your Compare method takes string arguments directly, but still there is no override involved. The generics stuff basically makes all the castings redundant. Regards,

          Luc Pattyn [Forum Guidelines] [My Articles]


          Sorry for any delays in replying, I currently don't get e-mail notifications.


          E 1 Reply Last reply
          0
          • E electriac

            This is my code that will not compile. class SubstringComparer : IComparer<string> { private int _offset, _len; public SubstringComparer(int offset, int len) { _offset = offset; _len = len; } public override int Compare(string x, string y) //--------------------------------------------------------- // I get no suitable method found to override Compare //--------------------------------------------------------- { return string.Compare(x, _offset, y, _offset, _len); } } private void button13_Click(object sender, EventArgs e) { ArrayList arraylist = new ArrayList(); for (int i = 0; i < listBox1.Items.Count; i++) { string temp = listBox1.Items[i].ToString(); if (temp.Length>65) { arraylist.Add(temp); } } Array.Sort(arraylist, new SubstringComparer(59, int.MaxValue)); listBox1.Items.Clear(); for (int i = 0; i < arraylist.Count; i++) listBox1.Items.Add(arraylist[i]); } } TNX for the help

            modified on Saturday, December 08, 2007 10:45:32 AM

            G Offline
            G Offline
            Guffa
            wrote on last edited by
            #12

            Oh, you are not using an array at all, but an ArrayList. That's a big difference. Then you need a comparer that handles objects: class SubstringComparer : IComparer { private int _offset, _len; public SubstringComparer(int offset, int len) { _offset = offset; _len = len; } public override int Compare(object x, object y) { return string.Compare((string)x, _offset, (string)y, _offset, _len); } } And use it like this: arraylist.Sort(new SubstringComparer(59, int.MaxValue));

            Experience is the sum of all the mistakes you have done.

            E 1 Reply Last reply
            0
            • G Guffa

              Oh, you are not using an array at all, but an ArrayList. That's a big difference. Then you need a comparer that handles objects: class SubstringComparer : IComparer { private int _offset, _len; public SubstringComparer(int offset, int len) { _offset = offset; _len = len; } public override int Compare(object x, object y) { return string.Compare((string)x, _offset, (string)y, _offset, _len); } } And use it like this: arraylist.Sort(new SubstringComparer(59, int.MaxValue));

              Experience is the sum of all the mistakes you have done.

              E Offline
              E Offline
              electriac
              wrote on last edited by
              #13

              Still will not compile

                  class SubstringComparer : IComparer
                  {
                      private int \_offset, \_len;
                      public SubstringComparer(int offset, int len)
                      {
                          \_offset = offset; \_len = len;
                      }
                      public override int Compare(object x, object y)
                          //-----------------------------------------------------------------------
                          //        On Compare I still get no suitable method to overide
                          //
                          //-----------------------------------------------------------------------
                      {
                          return string.Compare((string)x, \_offset, (string)y, \_offset, \_len);
                      }
                  }
              
                  private void button13\_Click(object sender, EventArgs e)
                  {
                      ArrayList arraylist = new ArrayList();
                      for (int i = 0; i < listBox1.Items.Count; i++)
                      {
                          string temp = listBox1.Items\[i\].ToString();
                          if (temp.Length>65)
                          {
                                  arraylist.Add(temp);
                          }
                      }
                      
                      arraylist.Sort(new SubstringComparer(59, int.MaxValue)); 
                    
                      listBox1.Items.Clear();
                      for (int i = 0; i < arraylist.Count; i++)
                          listBox1.Items.Add(arraylist\[i\]);
                  }
              
              G 1 Reply Last reply
              0
              • L Luc Pattyn

                Hi George, there are basically two ways (old and new): 1. if you want to sort an ArrayList in a special way, you need to create an object that implements IComparer (without < string> ) and knows how to compare two items. Its signature MUST be public int Compare(object obj1, object obj2) There is no override, by writing ": Icomparer" you promise to implement the formentioned method. And you need to cast the arguments to strings explicitly, as in: string str1=(string)obj1 2. Starting with .NET 2.0 you can also use generic collections. The code Guffa showed you is fine if you keep the strings in a generic list, that is a List< string>. In that case you inherit from IComparer< string> and your Compare method takes string arguments directly, but still there is no override involved. The generics stuff basically makes all the castings redundant. Regards,

                Luc Pattyn [Forum Guidelines] [My Articles]


                Sorry for any delays in replying, I currently don't get e-mail notifications.


                E Offline
                E Offline
                electriac
                wrote on last edited by
                #14

                Is it possible that my problem is associated with the fact that I am using .NET 3.5 and Visual 2008.

                L D 2 Replies Last reply
                0
                • E electriac

                  Still will not compile

                      class SubstringComparer : IComparer
                      {
                          private int \_offset, \_len;
                          public SubstringComparer(int offset, int len)
                          {
                              \_offset = offset; \_len = len;
                          }
                          public override int Compare(object x, object y)
                              //-----------------------------------------------------------------------
                              //        On Compare I still get no suitable method to overide
                              //
                              //-----------------------------------------------------------------------
                          {
                              return string.Compare((string)x, \_offset, (string)y, \_offset, \_len);
                          }
                      }
                  
                      private void button13\_Click(object sender, EventArgs e)
                      {
                          ArrayList arraylist = new ArrayList();
                          for (int i = 0; i < listBox1.Items.Count; i++)
                          {
                              string temp = listBox1.Items\[i\].ToString();
                              if (temp.Length>65)
                              {
                                      arraylist.Add(temp);
                              }
                          }
                          
                          arraylist.Sort(new SubstringComparer(59, int.MaxValue)); 
                        
                          listBox1.Items.Clear();
                          for (int i = 0; i < arraylist.Count; i++)
                              listBox1.Items.Add(arraylist\[i\]);
                      }
                  
                  G Offline
                  G Offline
                  Guffa
                  wrote on last edited by
                  #15

                  Silly me... It's an interface, so you don't override the methods. Just remove the override keyword.

                  Experience is the sum of all the mistakes you have done.

                  modified on Monday, December 10, 2007 7:05:57 PM

                  E 1 Reply Last reply
                  0
                  • E electriac

                    Is it possible that my problem is associated with the fact that I am using .NET 3.5 and Visual 2008.

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

                    Hi George, I don't know for sure about 3.5/2008 since I haven't used them yet. but there are some problems in the code you posted, as I pointed out before. For one you should drop the "override" then choose either an ArrayList (1. in my earlier reply) or List< string> (2) and make your Compare accordingly. :)

                    Luc Pattyn [Forum Guidelines] [My Articles]


                    Sorry for any delays in replying, I currently don't get e-mail notifications.


                    1 Reply Last reply
                    0
                    • E electriac

                      Is it possible that my problem is associated with the fact that I am using .NET 3.5 and Visual 2008.

                      D Offline
                      D Offline
                      DaveyM69
                      wrote on last edited by
                      #17

                      I haven't investigated to see if there are any new 'features' in this regard - but Generic Lists, IComparer and IComparable definately function as before and none of the 'normal' methods have changed in VS2008/.NET3.5 To be honest, so far I've had no problems coding exactly the same as I did with 2005 with all classes/interfaces etc.

                      E 1 Reply Last reply
                      0
                      • G Guffa

                        Silly me... It's an interface, so you don't override the methods. Just remove the override keyword.

                        Experience is the sum of all the mistakes you have done.

                        modified on Monday, December 10, 2007 7:05:57 PM

                        E Offline
                        E Offline
                        electriac
                        wrote on last edited by
                        #18

                        Guffa Many thanks for your help and persistence sticking with me until the code was working. I have now implemented the methods you suggested in numerous programs and it has proven a valuable addition. Thanks Again

                        1 Reply Last reply
                        0
                        • D DaveyM69

                          I haven't investigated to see if there are any new 'features' in this regard - but Generic Lists, IComparer and IComparable definately function as before and none of the 'normal' methods have changed in VS2008/.NET3.5 To be honest, so far I've had no problems coding exactly the same as I did with 2005 with all classes/interfaces etc.

                          E Offline
                          E Offline
                          electriac
                          wrote on last edited by
                          #19

                          I have found also that all my old code could migrate without problem.

                          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