Hi,

I need help on this asap please..

I need a program to build a console application to sort a list of names entered in an array in alphabetical order WITHOUT using array.Sort() Method....
please this is not an assaignment ,i have tried a lot but couldnot figure out
plzz plzz plzz help

thanks!

Recommended Answers

All 9 Replies

Welcome.
Did you work out? Please show your code.

hi i tried to use some logic but it didnt stuck my mind
I know this logic is not accurate but this is what i could think of

 string[] str = new string[] {"aaa","abc","acb"};
        int length = str.Length;
        string j;
        string k;

        for (int i = 0; i <= str.Length; i++)
        { 
          str[i] = j;
          str[i + 1] = k;
          if (j(i) > k(i))
          {
              str[i + 1] = str[i];
              str[i] = k;

          }

Hi ramy ,

can you tell me what is Icomparer interface

can you give me tha code

I don't have the code you can read about it, just googlize it. also for comparing strings you'll find a dozen of examples.

hi i tried to use some logic but it didnt stuck my mind
I know this logic is not accurate but this is what i could think of

 string[] str = new string[] {"aaa","abc","acb"};  
       int length = str.Length; // you dont need this variable
        string j;
        string k;

        for (int i = 0; i <= str.Length; i++)
        { 
          str[i] = j;
          str[i + 1] = k;
         if (j[i].CompareTo( k[i]))==1);
          {
              str[i + 1] = str[i];
              str[i] = k;

          }

NB!!i did not test this code. ihope it works.:)

NB!!i did not test this code. ihope it works.:)

lol, it infact does not. tons of syntax errors 9 to start out with, not to mention some of the variable paths don't make sense, first thing it does it set the index of the array to an empty variable, then each time set the next string in the array to whatever the last one was. everything is backwards and you are using index character references that are unneeded on the strings. here is a working code example, although it doesn't work well, it just doesn't have the errors.

string[] str = new string[] {"mar", "bab","fas"};//start with
//text that is acutally out of order!

int length = str.Length; // you dont need this variable
string j;
string k;

for (int i = 0; i < str.Length -1; i++)
{
j = str[i];//this was backwards
k = str[i + 1];//this was backwards
if (j.CompareTo(k)==1)//this was all messed up, 5 errors were here
{
str[i + 1] = str[i];
str[i] = k;
}
}

that works, kind of. comparing returns 1 of 3 values, -1 which means it should be before, 0 which means its the same, and 1 which means it should be after. for example, if j.compareto(k) ==1 then j should come after k.

this code leaves out what to do otherwise, but its written in a way to always compare the the next to the current. so it takes care of that for you. but the problem is, it won't work correctly in 1 pass, the for loop will need to be repeated until it runs twice with no change to make sure that its actually done. I will work on it a min and post a finished loop in a few.

here we go, this isn't perfect but it works. if you find that its not quite fully sorted, just increase the amount of loops in the main for loop. as many loops as strings seems to work fine. that's what I used here.

then here is the sort.

string[] str = new string[] { "kevin", "adam", "zenith", "taco", "brandy", "david" };
            string j;
            string k;
            for (int g = 0; g<=str.Length; g++)//default on as many loops as strings
            {
                for (int i = 0; i < str.Length - 1; i++)
                {
                    j = str[i];
                    k = str[i + 1];
                    if (j.CompareTo(k) == 1)
                    {
                        str[i + 1] = str[i];
                        str[i] = k;
                    }
                }
              
            }

have fun with it.

ok Thanks a lot for the help will work with the code and come back if i have any more queries..

Thnk u..reallly appreciate your help...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.