DeeGo
DeeGo

Reputation: 17

How to swap two strings in a single line in alphabetic order?

Good day all,

I want to swap two strings in a single line based on its alphabetic order.

Example:

Arun 2012, Gopi 2010, Dinesh 2012. Computer Networks, Tata McGraw Hill. 745:19-22

In this line author names Likely Arun, Dinesh, Gopi have to swap in alphabetical order and saved in a same file for reference.

How could it possible to swap exactly the string array?

Here is what I tried:

foreach (string strPara in strParaValue) 
{ 
   string[] strAuthorsPart = strPara.Split('.'); 
   string[] strAuthorslist = strAuthorsPart[0].Split(','); 
   string[] strAuthor = strAuthorslist[0].Split(' '); 
   if (strSplitValue[0].Contains(strAuthor[0].Trim())) 
   { 
      swt.WriteLine(strAuthor[0] + "\t"); 
   } 
   else 
   {
      swf.WriteLine(strAuthor[0] + "\t"); 
   }
}

Thanks DeeGo.

Upvotes: 0

Views: 803

Answers (3)

Chibueze Opata
Chibueze Opata

Reputation: 10054

You mean something like this?

string tosort = " Arun 2012, Gopi 2010, Dinesh 2012. Computer Networks, Tata McGraw Hill. 745:19-22";
string sortedAuthors = "";
string sortedTexts = "";
List<string> mylist = tosort.Split(new[]{',', '.'}).ToList<string>();
mylist.Sort();
mylist.ForEach(n => if(n.Substring.IndexOf(" ").sorted += n + ",");

This will produce the output

" 745:19-22, Arun 2012, Computer Networks, Dinesh 2012, Gopi 2010, Tata McGraw Hill,"

Based on your comment, try this:

string tosort = " Arun 2012, Gopi 2010, Dinesh 2012. Computer Networks, Tata McGraw Hill. 745:19-22";
string sortedAuthors = "";
string sortedTexts = "";
List<string> mylist = tosort.Split(new[] { ',', '.' }).ToList<string>();
mylist.Sort(); int i;
foreach (var n in mylist)
{
    if (Int32.TryParse(n.Substring(n.IndexOf(' ')).Trim(), out i))
    {
        sortedAuthors += (n + ", ");
    }
    else
    {
        sortedTexts += (n + ", ");
    }
}
string final = sortedAuthors + ", " + sortedTexts;

This will output:

, 745:19-22, Arun 2012, Computer Networks, Dinesh 2012, Gopi 2010, Tata McGraw Hill,

I believe you can handle the rest.

Upvotes: 1

Saeed Amiri
Saeed Amiri

Reputation: 22555

after you find strAuthorslist your task is easy:

string[] strAuthorsPart = strPara.Split('.'); 
string[] strAuthorslist = strAuthorsPart[0].Split(','); 
strAuthorslist.Sort();
var newAuthorsArrangement = String.Join(strAuthorslist, ",");
var updatedLine = newAuthorsArrangement  + "." +
                  strPara.SkipWhile(x=>x!='.').ToString();

I used .Net 4 SkipWhile method, but you can simply handle last part with .Net 1.1

Upvotes: 0

Oleg Golovkov
Oleg Golovkov

Reputation: 706

First you have to get the strings you are sorting in some container, like Array. For example string[] strings = s.Split(','). Be sure to get only the part with author names in your case. Then you can sort the result array in any convinient way, for example: Array.Sort(strings). Then create new string from them: string newline = string.Join(",",strings) and concat it with waht you had before. Remember that any operations with string modifications actually produce new string object, because strings are immutable, so no kind of "in-place" sort is possible

Upvotes: 0

Related Questions