Click here to Skip to main content
14,238,591 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hi All,

I have declared a variable as string and placed double values in that. e.g.
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:

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
Rate this:
Please Sign up or sign in to vote.

Solution 1

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[^]
   
Comments
Prasad_Kulkarni 27-Jun-12 3:48am
   
5'ed
Rate this:
Please Sign up or sign in to vote.

Solution 2

Hi ,
Check this
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
   
v2
Comments
Prasad_Kulkarni 27-Jun-12 3:48am
   
5'ed
Mohamed Mitwalli 27-Jun-12 4:10am
   
Thanks Prasad :)
Rate this:
Please Sign up or sign in to vote.

Solution 3

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:
string[] arrayOfStrings = new string[] { "128", "124", "122", "121", "120", "", "117" };
Array.Sort(arrayOfStrings);
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.
   
Rate this:
Please Sign up or sign in to vote.

Solution 4

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
List<string> </string>
to store all the value..

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


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>


////out put 


117
120
121
122
124
128
   

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100