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

C# 对字符串值内的数据进行排序,c#,string,linq,sorting,date-format,C#,String,Linq,Sorting,Date Format,包含特定格式的日期和整数的字符串:MM/dd/yyyy Number 基于上述情况,我希望: 如果日期相似,则添加编号 排序应基于日期,即升序 预期产出: 我知道这是可能的,如果我们根据分号进行拆分,然后使用“for循环”遍历值 但请给我建议linq解决方案 这应该有效: var elems = strData.Split(';') // First, split on semicolon .Select(s => s.Trim().Split(' ')) // then remove

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

基于上述情况,我希望:

如果日期相似,则添加编号 排序应基于日期,即升序 预期产出:

我知道这是可能的,如果我们根据分号进行拆分,然后使用“for循环”遍历值

但请给我建议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'/'yyyyy}{g.Suma=>a.n}替换为string.Format{0:MM'/'dd'/'yyyyy}{1},g.Key,g.Suma=>a.n

这应该可以:

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.Suma=>a.n}替换为string.Format{0:MM'/'dd'/'yyyyy}{1},g.Key,g.Suma=>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) + ")")); 

这是另一种方法

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) + ")")); 

还有Linq,我很确定你需要在分号上拆分,才能从这个字符串中找到任何意义。你能给我第一点的样本数据吗?你说的添加数字是什么意思?@derloopkat他想在日期后面的括号中添加数字,如果日期相同,基本上是一组数字elements@gopinath,以上提供的示例满足要求。你可以通过添加更多分号来创建这样的数据:还有Linq,我很确定你需要在分号上拆分,以便从该字符串中找到任何意义。你能给我第1点的示例数据吗?添加数字是什么意思?@derloopkat如果日期相同,他想在日期后面的括号中添加数字,基本上是一组elements@gopinath,以上提供的示例满足要求。您可以通过添加更多分号来创建此类数据: