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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Object array question

Object array question

Scheduled Pinned Locked Moved C#
helpalgorithmsdata-structurestestingbeta-testing
8 Posts 2 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.
  • T Offline
    T Offline
    T Willey
    wrote on last edited by
    #1

    I am sorting an array of object arrays (string,int). It will work fine is the string is only one character long, but if it is two it will error. I'm not sure why or how it errors. I have done some testing, but still don't understand how to fix it. Any help is appreciated. Thanks in advance. private object[,] SortByFirst (object[,] RevAr) { object[,] NewAr = new object[3,2]; for (int i = 0; i < (RevAr.Length / 2); ++i) { int NewArLoc = (RevAr.Length / 2); --NewArLoc; string TestStr = RevAr[i,0].ToString(); for (int j = 0; j < (RevAr.Length / 2); ++j) { if (i != j) { string tempStr = RevAr[j,0].ToString(); int intTest = string.Compare(TestStr, tempStr, true); if (TestStr.Length < tempStr.Length || (intTest < 0 && tempStr != "")) { --NewArLoc; } } } NewAr[NewArLoc,0] = RevAr[i,0]; NewAr[NewArLoc,1] = RevAr[i,1]; } //MessageBox.Show (NewAr[0,0].ToString() + NewAr[0,1].ToString() + "\n" + NewAr[1,0].ToString() + NewAr[1,1].ToString() + "\n" + NewAr[2,0].ToString() + NewAr[2,1].ToString()); //MessageBox.Show (NewAr[2,0].ToString()); return NewAr; } The inital array could look like { {"A",0} {"C",6} {"B",12} } This array would work, and would be returned like { {"A",0} {"B",12} {"C",6} } ps. I don't know how to post code correctly still, so sorry for the way my post looks. Tim

    G 1 Reply Last reply
    0
    • T T Willey

      I am sorting an array of object arrays (string,int). It will work fine is the string is only one character long, but if it is two it will error. I'm not sure why or how it errors. I have done some testing, but still don't understand how to fix it. Any help is appreciated. Thanks in advance. private object[,] SortByFirst (object[,] RevAr) { object[,] NewAr = new object[3,2]; for (int i = 0; i < (RevAr.Length / 2); ++i) { int NewArLoc = (RevAr.Length / 2); --NewArLoc; string TestStr = RevAr[i,0].ToString(); for (int j = 0; j < (RevAr.Length / 2); ++j) { if (i != j) { string tempStr = RevAr[j,0].ToString(); int intTest = string.Compare(TestStr, tempStr, true); if (TestStr.Length < tempStr.Length || (intTest < 0 && tempStr != "")) { --NewArLoc; } } } NewAr[NewArLoc,0] = RevAr[i,0]; NewAr[NewArLoc,1] = RevAr[i,1]; } //MessageBox.Show (NewAr[0,0].ToString() + NewAr[0,1].ToString() + "\n" + NewAr[1,0].ToString() + NewAr[1,1].ToString() + "\n" + NewAr[2,0].ToString() + NewAr[2,1].ToString()); //MessageBox.Show (NewAr[2,0].ToString()); return NewAr; } The inital array could look like { {"A",0} {"C",6} {"B",12} } This array would work, and would be returned like { {"A",0} {"B",12} {"C",6} } ps. I don't know how to post code correctly still, so sorry for the way my post looks. Tim

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

      What error message do you get? If you use framework 2.0 you could use a sorted dictionary instead: SortedDictionary<string, int> items = new SortedDictionary<string, int>(); items.Add("A", 0); items.Add("C", 6); items.Add("B", 12); Voila. Sorted.

      --- b { font-weight: normal; }

      T 1 Reply Last reply
      0
      • G Guffa

        What error message do you get? If you use framework 2.0 you could use a sorted dictionary instead: SortedDictionary<string, int> items = new SortedDictionary<string, int>(); items.Add("A", 0); items.Add("C", 6); items.Add("B", 12); Voila. Sorted.

        --- b { font-weight: normal; }

        T Offline
        T Offline
        T Willey
        wrote on last edited by
        #3

        It doesn't really stop the code, it's just that the finished product isn't correct if the string has more than one character. I'm still on framework 1.1. This code is use to update revision levels. So if the current revision is A, the new one is B. So if the current one is Z, the new one is AA. We show three revision levels on our drawings. Sometimes they are in the correct order, and sometimes they are not, so I'm sorting them first before I update the revisions. So say I have revisions 'AA', 'Z', 'Y' (in order top to bottom). It will say that the new revision is 'Z' and will order them 'Z', 'Y', 'Z', in this order from top to bottom, when it should be 'AB', 'AA', 'Z'. This is why I'm thinking something is wrong in the sorting code I posted.

        Tim

        G 1 Reply Last reply
        0
        • T T Willey

          It doesn't really stop the code, it's just that the finished product isn't correct if the string has more than one character. I'm still on framework 1.1. This code is use to update revision levels. So if the current revision is A, the new one is B. So if the current one is Z, the new one is AA. We show three revision levels on our drawings. Sometimes they are in the correct order, and sometimes they are not, so I'm sorting them first before I update the revisions. So say I have revisions 'AA', 'Z', 'Y' (in order top to bottom). It will say that the new revision is 'Z' and will order them 'Z', 'Y', 'Z', in this order from top to bottom, when it should be 'AB', 'AA', 'Z'. This is why I'm thinking something is wrong in the sorting code I posted.

          Tim

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

          If you want to sort them in that way, you have to first compare the length of the strings, then compare the values. If you just compare the values, "AA" comes before "Z".

          --- b { font-weight: normal; }

          T 2 Replies Last reply
          0
          • G Guffa

            If you want to sort them in that way, you have to first compare the length of the strings, then compare the values. If you just compare the values, "AA" comes before "Z".

            --- b { font-weight: normal; }

            T Offline
            T Offline
            T Willey
            wrote on last edited by
            #5

            I thought that is what I was doing here, and I wrong? int intTest = string.Compare(TestStr, tempStr, true); if (TestStr.Length < tempStr.Length || (intTest < 0 && tempStr != "")) { --NewArLoc; } I saying that the NewArLoc is equal the to the length of the array minus one. Then if the length of the string I'm working with 'TestStr' is shorter that the one I'm testing it against 'tempStr', or the value is less (value from string.Compare), then minus one. I want the lowest string value to be first in the array............. I see what you mean now. Let me see what I can come up with. My test line isn't correct. It will test the length, and then if 'TestStr' is longer, then it will test the value of the compare, which it shouldn't. I will have to rewrite that part. Thanks. Tim

            1 Reply Last reply
            0
            • G Guffa

              If you want to sort them in that way, you have to first compare the length of the strings, then compare the values. If you just compare the values, "AA" comes before "Z".

              --- b { font-weight: normal; }

              T Offline
              T Offline
              T Willey
              wrote on last edited by
              #6

              Thanks Guffa. Here is how I solved it, incase only looks at this. int intTest = string.Compare(TestStr, tempStr, true); if (TestStr.Length < tempStr.Length || (intTest < 0 && tempStr != "" && TestStr.Length == tempStr.Length)) { --NewArLoc; } This is the portion I added to my check. && TestStr.Length == tempStr.Length Tim

              G 1 Reply Last reply
              0
              • T T Willey

                Thanks Guffa. Here is how I solved it, incase only looks at this. int intTest = string.Compare(TestStr, tempStr, true); if (TestStr.Length < tempStr.Length || (intTest < 0 && tempStr != "" && TestStr.Length == tempStr.Length)) { --NewArLoc; } This is the portion I added to my check. && TestStr.Length == tempStr.Length Tim

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

                You can make it a bit more efficient by only doing the compare when needed, and check the length of tempStr instead of comparing it to an empty string: if (TestStr.Length < tempStr.Length || (TestStr.Length == tempStr.Length && tempStr.Length > 0 && string.Compare(TestStr, tempStr, true) < 0)) { NewArLoc--; } As the condition is evaluated using shortcut, the compare is only done if the lengths are equal and tempStr is not empty.

                --- b { font-weight: normal; }

                T 1 Reply Last reply
                0
                • G Guffa

                  You can make it a bit more efficient by only doing the compare when needed, and check the length of tempStr instead of comparing it to an empty string: if (TestStr.Length < tempStr.Length || (TestStr.Length == tempStr.Length && tempStr.Length > 0 && string.Compare(TestStr, tempStr, true) < 0)) { NewArLoc--; } As the condition is evaluated using shortcut, the compare is only done if the lengths are equal and tempStr is not empty.

                  --- b { font-weight: normal; }

                  T Offline
                  T Offline
                  T Willey
                  wrote on last edited by
                  #8

                  True. Thanks of the tips Guffa. I'm just starting, so help/tips are always appreciated.

                  Tim

                  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