Tech Off Thread

6 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

how to sort in ascending order multiple decimal numbers in list using c#.net

Back to Forum: Tech Off
  • User profile image
    TechSuport

    0
    0.0
    1
    1.331.33,
    11.21.051,
    05
    .1.10,
    0.0.0.1
    12

  • User profile image
    GreyLensman

    If you are working on a class project where you have been instructed to write a sort program, this is not your answer.

     

    The simplest way to sort a list of decimal numbers is to have the framework do it for you.

    List<decimal> numberList = new List<decimal>() { 2.54, .035, 75.23, 12.01, 23.35, 0.1234 }

    numberList.Sort();

    Thats it.  Stick your numbers inside the { } symbols and let the framework do the magic for you.

  • User profile image
    figuerres

    , TechSuport wrote

    0
    0.0
    1
    1.331.33,
    11.21.051,
    05
    .1.10,
    0.0.0.1
    12

    That list is NOT a list of decimal numbers.

    If each of them is one item then you need a class that handles what that item is and what values are legal, in creating that class you implement the code for the standard compare functions and then a.net can sort that list.

  • User profile image
    vesuvius

    @TechSuport: figuerres is right, you are not dealing with decimals in the example you supplied in the initial post. Since your name is TechSuport one would guess at this being a list of IP addresses.

    figuerres advice is what you need, something like

    string addrString = "192.168.0.1";
    IPAddress address;
    if (IPAddress.TryParse(addrString, out address)) {
           //Valid IP, with address containing the IP
    } else {
           //Invalid IP
    }

     

  • User profile image
    GreyLensman

    Perhaps this might help you get some ideas.  Its a pseudo console app in C#

     

       public class Program : IComparer<System.Net.IPAddress>
        {
            static void Main(string[] args)
            {
                var iaList = new List<System.Net.IPAddress>();
                foreach (string arg in args)
                {
                    string argWithoutComma = arg.Replace(oldValue:",", newValue: "");
                    System.Net.IPAddress address;
                    if (System.Net.IPAddress.TryParse(argWithoutComma, out address) == true)
                    {
                        iaList.Add(address);
                    }
                }
                if ((iaList != null) &&
                    (iaList.Count > 0))
                {
                    if (iaList.Count > 1)
                    {
                        iaList.Sort(new Program());
                    }
                    System.Diagnostics.Debug.WriteLine(message: "Results in sorted order:");
                    foreach (System.Net.IPAddress ia in iaList)
                    {
                        System.Diagnostics.Debug.WriteLine(ia.ToString());
                    }
                }
                else
                {
                    System.Diagnostics.Debug.WriteLine(message: "No valid address information entered");
                }
            }
    
            public int Compare(IPAddress x, IPAddress y)
            {
                return string.Compare(x.ToString(), y.ToString());
            }
        }

  • User profile image
    spivonious

    String.Split('.')  to get the pieces of each entry and then apply your sorting rules. There's nothing built into .NET to do this for you.

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.