简体   繁体   中英

Int.Parse and Sorting

I have the following code..

var strings = new[] { 
"FD1","FD5","FD10","FD102","FD105","FD10","FD32","FD80", "FD31", "FD21", "FDnon"
};
        strings = strings.Select(str => new
        {
            str,
            num = int.Parse(String.Concat(str.Trim('F', 'D'))),
        })
        .OrderBy(x => x.num)
        .Select(x => x.str)
        .ToArray();

However this fails when it gets to "FDnon" as there are no numbers in it,

How do I get this to work with "FDnon" sorted at the top?

If you want custom ordering, supply your custom SortMethod

var sorted = strings
.OrderBy(SpecialSort)
.ToList();

public static int SpecialSort(string value)
{
    int sortOrder = 0;
    string numberPart = value.Trim('F', 'D');
    int.TryParse(numberPart, out sortOrder);
    return sortOrder;
}

Edit: Changed solution to account for the sorting of numbers in String.

var result = strings.OrderBy(x =>
                         {
                             int y = int.MinValue;
                             int.TryParse(x.Substring(2), out y);

                             return y;
                         });

If you want FDnon to be presented at the top you can use something like:

strings = strings.Select(str => new
    {
        str,
        num = str=="FDnon" ? Int32.MaxValue : Int32.Parse(String.Concat(str.Trim('F', 'D')))
    })
    .OrderBy(x => x.num)
    .Select(x => x.str)
    .ToArray();

This code just skips FDnon conversion. If you want to allow other values you should be more specific on what are you going to accept.

You need a more sophisticated parse that converts FDnon into an integral value that will sort last (in this case Int32.MaxValue would do).

Something like:

var res = strings.Select(s => {
            var numPart = s.Trim('F', 'D');
            var i;
            if (!Int32.TryParse(numPart, out i)) {
               i = Int32.MaxValue;
            }
            return new {
              str = s,
              num = i
            };
          }.OrderBy …

If the strings always start with two letters, you could also use Substring(2) . To check if there's a numeric part, you can use Enumerable.All(Char.IsDigit) (assumes that all chars are digits after the first two lewtters):

strings = strings.Select(str => new
{
    str,
    num = str.Substring(2).All(Char.IsDigit) ? int.Parse(str.Substring(2)) : int.MinValue
})
.OrderBy(x => x.num)
.Select(x => x.str)
.ToArray();

DEMO

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM