I have strinng emp=""2,5,4,3,9,55,77;
like this and want to sort in descorder
2,,3,4,5,9,55,77
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Khargesh RajputPosted Oct 16, 2013, 5:33 AM
tthanks
string sort1 = "";
string sort = "";
string input = empoyee1;
string[] stringArray = input.Split(new char[] { ',' },
StringSplitOptions.RemoveEmptyEntries);
int length = stringArray.Length;
int[] intArray = new int[length];
for (int i = 0; i < length; i++)
{
try
{
intArray[i] = Convert.ToInt32(stringArray[i]);
}
catch (Exception)
{
// ignore
}
}
Array.Sort(intArray);
foreach (int j in intArray)
{
sort = j + ",";
sort1 = sort1 + sort;
}
VulpesPosted Oct 16, 2013, 5:23 AM
public static string Join
For .NET 3.5 you have to convert to either a string[] or an object[], or do the concatenation yourself using foreach.
Sanjeeb LenkaPosted Oct 16, 2013, 5:17 AM
i checked jignesh solution it working fine for me.
you can try this also:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace IfExample
{
class SortString
{
static void Main()
{
string emp = "44,87,555,777,666";
string[] emps = emp.Split(',');
var query = from no in emps
orderby Convert.ToInt32(no)
select no;
string s = "";
foreach (String sa in query)
{
s += sa + ",";
}
Console.WriteLine(s);
}
}
}
Jignesh TrivediPosted Oct 16, 2013, 5:05 AM
hi,
try following code
string gg = "2,55,4,3,77,9,5";
//string pp = string.Join(",", gg.Split(new char[] { ',' }).OrderBy(f1 => Convert.ToInt32(f1)));
string emp = "";
var dd = gg.Split(',').OrderBy(f1 => Convert.ToInt32(f1));
foreach(string pp in dd)
{
emp += "," + pp;
}
emp = emp.Substring(1);
VulpesPosted Oct 16, 2013, 5:01 AM
Khargesh RajputPosted Oct 16, 2013, 4:51 AM
Ur code is right but it returns
string 44,87,555,777,666 to answer as
87,777,666,555
it compares87first
Jignesh TrivediPosted Oct 16, 2013, 4:47 AM
this is working fine for me.. :)
string gg = "2,55,4,3,77,9,5";
string pp = string.Join(",", gg.Split(new char[] { ',' }).OrderBy(f1 => Convert.ToInt32(f1)));
Khargesh RajputPosted Oct 16, 2013, 4:43 AM
Jignesh TrivediPosted Oct 16, 2013, 4:37 AM
using System;
using System.Linq;
Khargesh RajputPosted Oct 16, 2013, 4:34 AM
and error the best overloaded method match for string join
Jignesh TrivediPosted Oct 16, 2013, 4:27 AM
Is the error in my code?
if yes than please provide inforamtion like which framework version used by u...
my code is work fine for framework 3.5 and above...
Khargesh RajputPosted Oct 16, 2013, 4:22 AM
error
Posted Oct 16, 2013, 4:22 AM
Jignesh TrivediPosted Oct 16, 2013, 4:00 AM
i think you are looking solution in c#
try...
string gg = "2,55,4,3,77,9,5";
string pp = string.Join(",",gg.Split(new char[] { ',' }).OrderBy(f1 => Convert.ToInt32(f1)));
hope this will help you.