Click here to Skip to main content
16,021,125 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I have declared a variable as string and placed double values in that. e.g.
C#
string Numbers=string.Emtpy;
Numbers="128";
Numbers="124";
Numbers="122";
Numbers="121";
Numbers="120";
Numbers="";
Numbers="117";


Now I want to sort this variable as on the basis of no values.
I want result as:

C#
Numbers="";
Numbers="117";
Numbers="120";
Numbers="121";
Numbers="122";
Numbers="124";
Numbers="128";


Please help

Mohd Wasif
Posted
Updated 26-Jun-12 22:04pm
v4
Comments
SoMad 27-Jun-12 3:44am    
You have not placed values in your string. After the last assignment, you have Numbers == "117". You only have a single string variable, what you want to use is something like an array or list of strings.

Soren Madsen

Not sure if you are exactly looking for this, nevertheless have a look at below article in CP:
Numeric String Sort in C#[^]
http://www.dotnetperls.com/sort-number-strings[^]
 
Share this answer
 
Comments
Prasad_Kulkarni 27-Jun-12 3:48am    
5'ed
Hi ,
Check this
C#
string[] Numbers = new string[6];
        Numbers[0] = "128";
        Numbers[1] = "";
        Numbers[2] = "122";
        Numbers[3] = "121";
        Numbers[4] = "120";
        Numbers[5] = "117";
        var res = (from x in Numbers
                   orderby x ascending
                   select x).ToList();
        GridView1.DataSource = res;
        GridView1.DataBind();

Best Regards
M.Mitwalli
 
Share this answer
 
v2
Comments
Prasad_Kulkarni 27-Jun-12 3:48am    
5'ed
Mohamed Mitwalli 27-Jun-12 4:10am    
Thanks Prasad :)
That isn't going to work. Your code creates a string, then overwrites the current value repeatedly, leaving only the final "117" value.

Instead, look at using a List or an Array to hold multiple strings:
C#
string[] arrayOfStrings = new string[] { "128", "124", "122", "121", "120", "", "117" };
Array.Sort(arrayOfStrings);
C#
List<string> listOfStrings = new List<string>();
listOfStrings.Add("128");
listOfStrings.Add("124");
listOfStrings.Add("122");
listOfStrings.Add("121");
listOfStrings.Add("120");
listOfStrings.Add("");
listOfStrings.Add("117");
listOfStrings.Sort();
The list is more flexible, as you don't need to know the number of entries at any time - you can just add more.
 
Share this answer
 
AS you are showing your proble, that will not store all the value which you want, bacase the last value will over write the previous one ...

as concern to you problem, i think you need to use the
C#
List<string> </string>
to store all the value..

And on the basis of that you can find the result like


C#
List<string> Numbers = new List<string>();
            Numbers.Add("128");
            Numbers.Add("124");
            Numbers.Add("122");
            Numbers.Add("121");
            Numbers.Add("120");
            Numbers.Add("");
            Numbers.Add("117");
            var lst = from x in Numbers orderby x select x;

            foreach (var VARIABLE in lst)
            {
                Console.WriteLine(VARIABLE);
            }</string></string>


C#
////out put 


117
120
121
122
124
128
 
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