简体   繁体   中英

How can i sort List<string> by digits lowest to highest when the digits are part of the strings?

I have this sort method:

public List<string> SortList(List<string> thread)
        {
            thread = thread
    .OrderBy(str =>
    {
        var match = Regex.Match(str, @"^([-+]?\d+)");
        return match.Success ? int.Parse(match.Groups[1].Value) : int.MaxValue;
    })
    .ToList();
            responsers.Add(new List<string>(thread));
            return thread;
        }

And this is the method that call this SortList:

public List<string> GetResponsers(string contents)
        {
            string responser = "";
            List<string> threadList = new List<string>();
            int f = 0;
            int startPos = 0;
            while (true)
            {
                string firstTag = "<FONT CLASS='text16b'>";
                string lastTag = "&n";
                f = contents.IndexOf(firstTag, startPos);
                if (f == -1)
                {
                    break;
                }
                int g = contents.IndexOf(lastTag, f);
                startPos = g + lastTag.Length;
                responser = contents.Substring(f + 22, g - f - 22);
                threadList.Add(responser);
            }
            SortList(threadList);
            return threadList;
        }

The method GetResponsers get content of html file. And i'm parsing some stuff from it. In the end i have a List called: threadList

This method is calling each time with another content and parse stuff from it. For example in the end in this case threadList contain 9 items. The first item in index 0 is the title so it dosent have a number so i don't want to touch it to leave it in index 0 for exampe:

Hello world ?? (*)

Now in index 1 i have also text but with a number before it: "1. this is new" In index 2 i have also text with a number: "4. hello" And so on untill the lasti nde: "2. last item"

The problem is that in index 1 i can have "3. fgfgfg" In index 3 "6. fgfdghjj"

And i want to sort the List by the digits but the digits are part of each string !

In index 1 for example i have "3. hello world" The number 3 is not int. The number 3 is string.

I used a breakpoint and the SortList is not working good it's not sorting the List each time by the digits. In the end i want the List threadList to be only strings but the digits to be from first digit to last:

"this is the title"
"1. hello"
"2. hello world"
"3. this is third item"
"4. fourth item digits are strings"
"5. this is a test"
"6. this is after sorted"
"7. this is the last item"

The parsing method GetResponsers is working as i wanted the problem is how to sort the List by the digits when the digits are part of the strings in each index.

This code would work for positive single digits at the beginning:

public List<string> SortList(List<string> thread)
{
    var first = thread.Take(1);
    var ordered = thread.Skip(1).OrderBy(s=>char.GetNumericValue(s[0]));
    thread = first.Concat(ordered).ToList();
    return thread;
}

You should consider making this method returning just void btw. So either this

public void SortList(List<string> thread)
{
    var first = thread.Take(1);
    var ordered = thread.Skip(1).OrderBy(s=>char.GetNumericValue(s[0]));
    thread = first.Concat(ordered).ToList();
}

or my favourite

public List<string> SortList(List<string> thread)
{
    var first = thread.Take(1);
    var ordered = thread.Skip(1).OrderBy(s=>char.GetNumericValue(s[0]));
    return first.Concat(ordered).ToList();
}

although the last one needs you to change the calling code to return SortList(threadList);

Try this:

List<string> list; // this is your input list

var firstRow = list.Take(1);
var orderedRows = list.Skip(1)
    .OrderBy(s=>Int32.Parse(s.Split(' ')[0].TrimEnd('.')));
var result = firstRow.Concat(orderedRows).ToList();

If you like shorter, but less readable code:

var result = list.Take(1)
    .Concat(list.Skip(1)
    .OrderBy(s => Int32.Parse(s.Split(' ')[0].TrimEnd('.')))).ToList();

This way you are not limited to strings with one-digit number at the start.

All your input strings have to start with a number followed by a dot and a space, like so:

"1. "

Example input:

"this is the title"
"6. this is after sorted"
"1. hello"
"2. hello world"
"82. fourth item digits are strings"
"3. this is third item"
"5. this is a test"
"731. this is the last item"

And output:

"this is the title"
"1. hello"
"2. hello world"
"3. this is third item"
"5. this is a test"
"6. this is after sorted"
"82. fourth item digits are strings"
"731. this is the last item"

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