Click here to Skip to main content
15,894,460 members
Please Sign up or sign in to vote.
2.00/5 (4 votes)
See more:
Hi all.
I have two strings one is like (3,4,2,5,6) and another is like (name3,name4,name2,name5,name6).
I have done like this
string[] test=string1.Split(',');//(3,4,2,5,6)
test.Sort();//(2,3,4,5,6)
string1=String.join(",",test);

but I need to order second string also how it possible and
here I need to get values as(2,3,4,5,6)=>(name2,name3,name4,name5,name6) can any one help me please
thanks in advance
Posted
Updated 6-Apr-11 22:57pm
v2
Comments
Sergey Alexandrovich Kryukov 7-Apr-11 5:06am    
Just do it. Explain what's your problem if you do not succeed. This is how it can work.
Why do you think anyone would volunteer to do this boring work for you? Where is your contribution?
--SA
tulasiram3975 7-Apr-11 6:04am    
i told you my problem this is not A small task to do myself here second string elements not in order that means name3 will be jhon, name4 will be Pallini and so on here my intention is string1 and string2 must match each other sooo how can i sort second array if i do sort still order will miss.
tulasiram3975 7-Apr-11 6:22am    
am sorry for asking this small question

While I fully agree with SA, I cannot help myself exploiting the power of the framework:
C#
string string1 = "3,4,2,5,6";
string string2 = "name3,name4,name2,name5,name6";
string[] split1 = string1.Split(',');
string[] split2 = string2.Split(',');
Array.Sort(split1, split2);

:-)
 
Share this answer
 
Comments
tulasiram3975 7-Apr-11 6:01am    
thanks for reply but here second string elements not in order that means name3 will be jhon, name4 will be Pallini and so on here my intention is string1 and string2 must match each other sooo
how can i sort second array if i do sort still order will miss.
CPallini 7-Apr-11 6:49am    
The above code orders the names in string2 using string1 'items' as keys. If it is not your original intention, then I didn't get you: please elaborate your requirements, your comment isn't clear (maybe an example will help).
tulasiram3975 7-Apr-11 8:42am    
am sorry for that...
but your solution is good for that question
thank you
Just to understand..

You have 2 string arrays.

One string array with Ids = { 2, 3, 8, 1 }
Another string array with Names = { "Anna", "Kasper", "John", "Peter" }

Logic Id = 2, is Name = "Anna". Id = 3, is Name = "Kasper" etc. Right ?


Do something like:
SortedDictionary<string,> myList = new SortedDictionary<string,>
for (int count = 0; count < Ids.Length; count++)
{
  if (!myList.ContainsKey(Ids[count]))
  {
    myList.Add(Ids[count], Names[count]);
  }
}
 
Share this answer
 
Comments
tulasiram3975 7-Apr-11 8:40am    
Thank You But I tried Like concatenated two strings as 2->anna,3->kasper,8->jhon,1->peter sended to array after that sorted the array (in my way) but your code seems good .
thank you
Kim Togo 7-Apr-11 8:45am    
Thanks.
Do you what the sorting to happen in the names?
Not quite following what you want to achieve, but at a guess, you want to link the values to the names?

Why not create a Class and have a generic List, that way you can sort it.
C#
public class MyClass
  {
      public int Id { get; set; }
      public string Name { get; set; }
      public MyClass()
      {
      }
      public MyClass(int id, string name)
      {
          Id = id;
          Name = name;
      }
  }



That way when you are populating your Id and name string (Which is what I am guessing you are)

You can have a List of MyClass...

C#
public List<MyClass> MyClasses = new List<MyClass>();
MyClasses.Add(new MyClass(1, "Name1"));
MyClasses.Add(new MyClass(2, "Name2"));
MyClasses.Add(new MyClass(3, "Name3"));
MyClasses.Add(new MyClass(4, "Name4"));
MyClasses.Sort();


You can then even create your own sorting methods.
 
Share this answer
 
Comments
tulasiram3975 7-Apr-11 6:05am    
Dear Sir,
i think this is not a correct way, why should i create a generic class for small amount of code
Also, I'm not quite sure what you're trying to achieve, but don't see the need for the hostile response you got in asking.

Strings by default are sorted the normal way - typically according to the ASCII set. If you want to order them in some different manner, the best way to do it is to create a class that implements IComparer, which has just a single function for comparing them (perhaps by the last character or something).

Pass an instance of your class to the sort method and you should be in business.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900