对字符串值内的数据进行排序

Sort data within string value

包含特定格式的日期和整数的字符串:MM/dd/yyyy(数字)

string strData = "01/23/2017 (5); 01/16/2017 (2);01/24/2017 (6);01/16/2017 (5);01/23/2017 (10)";

基于以上我想要以下内容:

  1. 如果日期相似则添加编号
  2. 排序应基于日期,即升序

预期输出:

strData = "01/16/2017 (7);01/23/2017 (15);01/24/2017 (6)";    

我知道这是可能的,如果我们根据分号拆分然后使用 'for-loop' 遍历值。

但请建议我 linq 解决方案。

这应该有效:

var elems = strData.Split(';') // First, split on semicolon
  .Select(s => s.Trim().Split(' ')) // then remove the extra space at the end of each element, and split again on the space
  .Select(s => new { d = DateTime.ParseExact(s[0], "MM/dd/yyyy", CultureInfo.InvariantCulture), n = int.Parse(s[1].Replace("(", "").Replace(")", "")) }) // here, we create a temp object containing the parsed date and the value
  .GroupBy(o => o.d) // group by date
  .OrderBy(g => g.Key) // then sort
  .Select(g => $"{g.Key:MM'/'dd'/'yyyy} ({g.Sum(a => a.n)})"); // and finally build the resulting string

然后您可以使用以下方法构建最终字符串:

string.Join(";", elems);

此答案使用 C# 6 内插字符串。如果使用旧版本的语言,请将 $"{g.Key:MM'/'dd'/'yyyy} ({g.Sum(a => a.n)})" 替换为 string.Format("{0:MM'/'dd'/'yyyy} ({1})", g.Key, g.Sum(a => a.n))

这是另一种方法

string strData = "01/23/2017 (5); 01/16/2017 (2);01/24/2017 (6);01/16/2017 (5);01/23/2017 (10)";
string result = string.Join(";", strData.Split(';')
          .Select(x => new { 
              Date = DateTime.ParseExact(x.Trim().Split()[0], "MM/dd/yyyy", CultureInfo.InvariantCulture), 
              Count = int.Parse(x.Trim().Split()[1].Trim('(', ')')) })
          .GroupBy(x => x.Date)
          .OrderBy(x => x.Key)
          .Select(x => x.Key.ToString("MM/dd/yyyy") + " (" + x.Sum(y => y.Count) + ")"));