-6

可能重复:
按 LINQ C# 中出现的单词对列表进行排序

我在这里发布了一个类似的问题,并得到了一个不起作用的答案。这是我需要排序的数据。

  ID     Title
  ---    ------
  1      JCB Excavator - ECU P/N: 728/35700
  2      Geo Prism 1995 Geo cart Geo001 -Geo ABS #16213899
  3      Geo Prism 1995 - Geo ABS #16213899
  4      Geo Prism 1995 - ABS #16213899
  5      Wie man BBA reman erreicht
  6      this JCB test JCB
  7      Ersatz Airbags, Gurtstrammer und Auto Körper Teile

我的搜索数据就像“JCB GEO”。我想对它进行排序,使数据变得像

  ID     Title
  ---    ------
  2      Geo Prism 1995 Geo cart Geo001 -Geo ABS #16213899
  3      Geo Prism 1995 - Geo ABS #16213899
  4      Geo Prism 1995 - ABS #16213899
  6      this JCB test JCB
  1      JCB Excavator - ECU P/N: 728/35700
  5      Wie man BBA reman erreicht
  7      Ersatz Airbags, Gurtstrammer und Auto Körper Teile

首先出现的行是数据时间最长的行。所以对于GEO,这个词在大多数行中都有最长的时间。ID 2 有最大时间,单词 GEO,下一行有 2 个 Geo 单词...

我说我发布了这个问题并在下面得到了答案,这不起作用。

List<SearchResult> list = new List<SearchResult>() {
  new SearchResult { Description = "JCB Excavator - ECU P/N: 728/35700" },
  new SearchResult { Description = "Geo Prism 1995 - ABS #16213899" },
  new SearchResult { Description = "Geo Prism 1995 - ABS #16213899" },
  new SearchResult { Description = "Geo Prism 1995 - ABS #16213899" },
  new SearchResult { Description = "Wie man BBA reman erreicht" },
  new SearchResult { Description = "this test JCB" },
  new SearchResult { Description = "Ersatz Airbags, Gurtstrammer und Auto Körper  Teile"   }            
};
var wordsToFind = "Geo JCB".Split();
var values = list.Select(x => new { SearchResult = x, Count = x.Description.Split(' ')
  .Where(c => wordsToFind .Contains(c)).Count() })
  .OrderByDesending(x => x.Count)
  .Select(x => x.SearchResult);
4

1 回答 1

0

也许你可以这样做?我用来测试的主控制台程序

static void Main(string[] args)
        {
            var list = SearchResult.GetResult();
            var wordArray = "Geo,JCB".Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            list.Sort((a, b) => {
                int sum1 = 0, sum2=0;
                foreach (var item in wordArray)
                {
                    sum1 += Regex.Matches(a.Description, item).Count;
                    sum2 += Regex.Matches(b.Description, item).Count;
                }

                if (sum1 < sum2) return 1;
                else if (sum1 > sum2) return -1;
                else return 0;
            });
            foreach (var item in list)
            {
                Console.WriteLine(item.Description);
            }
            Console.ReadKey();
        }

搜索结果类

public class SearchResult
    {
        public string Description { get; set; }
        public static List<SearchResult> GetResult()
        {
            var list = new List<SearchResult>()
            {
                new SearchResult(){Description = "JCB Excavator - ECU P/N: 728/35700"},
                new SearchResult(){Description = "Geo Prism 1995 Geo cart Geo001 -Geo ABS #16213899"},
                new SearchResult(){Description = "Geo Prism 1995 - Geo ABS #16213899"},
                new SearchResult(){Description = "Geo Prism 1995 - ABS #16213899"},
                new SearchResult(){Description = "Wie man BBA reman erreicht"},
                new SearchResult(){Description = "this JCB test JCB"},
                new SearchResult(){Description = "Ersatz Airbags, Gurtstrammer und Auto Körper  Teile"}
            };
            return list;
        }
    }


根据您的要求返回正确的输出...

Geo Prism 1995 Geo cart Geo001 -Geo ABS #16213899
Geo Prism 1995 - Geo ABS #16213899
this JCB test JCB
Geo Prism 1995 - ABS #16213899
JCB Excavator - ECU P/N: 728/35700
Ersatz Airbags, Gurtstrammer und Auto Körper  Teile
Wie man BBA reman erreicht
于 2012-07-09T11:06:04.830 回答