Hi, I'm a newbie. I want to insert a string into a sorted string array, in a way that it remains sorted. The only solution I reached is to add a string and then sort it again. But is there a way to insert a string into the array in the correct position? Please guide me! here is my code:
view source
print?
01 namespace Add_To_String
02 {
03 class Program
04 {
05 static void Main(string[] args)
06 {
07 string[] Names = new string[] { "Ali", "Behrooz", "Darya", "Farbod", "Hamid" };
08 string[] List = new string[Names.Length + 1];
09 Names.CopyTo(List, 0);
10 List.SetValue("Chakave", Names.Length);
11 Array.Sort(List);
12 foreach (string S in List)
13 {
14 Console.WriteLine(S.ToString());
15 }
16 Console.ReadLine();
17
18 }
19 }
20 }
1 reply to this topic
#1
Posted 29 December 2010 - 11:13 AM
|
|
|
#2
Posted 29 December 2010 - 01:02 PM
Since you already know all of the items in the list, you could just start out with the extra item in the array.
What are your real requirements?
A List<string> is a better structure for this sort of operation, as it doesn't need recreating every time you want to insert an object.
What are your real requirements?
A List<string> is a better structure for this sort of operation, as it doesn't need recreating every time you want to insert an object.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account

Back to top









