对字符串中的信息进行排序?

时间:2015-07-16 15:57:28

标签: asp.net sorting

好吧,假设我在数组中有值,这些值最初位于ASP.NET网站上显示的标签中。

  

start25 middle15 finish2 end4 halfway8 notthereyet0 almost13 gotit45

它们目前都被此标签中的断行隔开,我使用substring从字符串中专门获取这些数字。

我已经考虑了很长一段时间并想要这样,除了每个字符串有断裂线。

  
    

notthereyet0 finish2 end3 halfway8 near13 middle15 start25 gotit45

  

我怎么去,重新排序? (请记住,断裂线在那里,我希望每次都在新线上,并保持文本在前面?)。

2 个答案:

答案 0 :(得分:2)

var yourvar="start25 middle15 finish2 end4 halfway8 notthereyet0 nearly13 gotit45"
    .Replace(" ","\n");
var result=String.Join("\n",yourvar.Split('\n')
  .OrderBy(x=>Convert.ToInt32(Regex.Match(x, "(\\d+)").Value)));

结果:

notthereyet0 finish2 end4 halfway8 nearly13 middle15 start25 gotit45

答案 1 :(得分:0)

为了我的娱乐。

class Program
{
    static void Main(string[] args)
    {
        string s = "start25 middle15 finish2 end4 halfway8 notthereyet0 nearly13 gotit45";

        Console.WriteLine(s); // start25 middle15 finish2 end4 halfway8 notthereyet0 nearly13 gotit45

        s = new Test().Sort(s);

        Console.WriteLine(s); // notthereyet0 finish2 end4 halfway8 nearly13 middle15 start25 gotit45
        Console.ReadLine();
    }
}

class Test
{
    public string Sort(string str)
    {
        List<string> items = new List<string>();

        int length = str.Length;
        int offset = 0;
        int pos;

        for (pos = 0; pos < length; pos++)
        {
            char c = str[pos];

            if (c == ' ')
            {
                if (pos != offset)
                {
                    items.Add(str.Substring(offset, pos - offset));
                }

                offset = pos + 1;
            }
        }

        if (pos != offset)
        {
            items.Add(str.Substring(offset, pos - offset));
        }

        List<Entry> entries = GetEntries(items);

        entries.Sort(new Comparison<Entry>(Entry.CompareByNumber));

        return Format(entries, str.Length);
    }

    private string Format(List<Entry> entries, int size)
    {
        StringBuilder sb = new StringBuilder(size);

        int count = entries.Count;

        for (int i = 0; i < count; i++)
        {
            if (i > 0)
            {
                sb.Append(' ');
            }

            sb.Append(entries[i].Name);
            sb.Append(entries[i].Number);
        }

        return sb.ToString();
    }

    private List<Entry> GetEntries(List<string> items)
    {
        List<Entry> entries = new List<Entry>(items.Count);

        foreach (string item in items)
        {
            entries.Add(Disect(item));
        }

        return entries;
    }

    private Entry Disect(string item)
    {
        Entry result = new Entry();

        int length = item.Length;
        int pos;

        for (pos = 0; pos < length; pos++)
        {
            char c = item[pos];

            if (Char.IsDigit(c))
            {
                if (pos == 0)
                {
                    throw new Exception("Name missing in item.");
                }

                result.Name = item.Substring(0, pos);

                Int32 n;

                if (!Int32.TryParse(item.Substring(pos), out n))
                {
                    throw new Exception("Invalid number section.");
                }

                result.Number = n;

                break;
            }
        }

        if (pos == length)
        {
            throw new Exception("Number missing in item.");
        }

        return result;
    }

    private class Entry
    {
        public string Name;
        public Int32 Number;

        public static int CompareByNumber(Entry a, Entry b)
        {
            return (a.Number - b.Number);
        }
    }
}