3

I am working on a problem in c#. I have an array of objects and one key object. There are five properties of an object:

  1. Group
  2. country
  3. Service
  4. Industry
  5. Technology

I want to arrange objects such that most matching object with key object will be displayed first. I want to arrange that data in following order -

  1. It will get objects of same group as group of key object.
  2. If objects of same group are found then these will be ordered according to country. i.e. If same parameter value is found then order will be decided according to next parameter.
  3. Same process will be followed for objects with different groups also.

I have got one solution: I created 32 linq queries with each permutation and combination of 5 parameters, then I merged those 32 results one by one. This solution gives me desired results but this solution needs a lot of processing. Please provide any shorter solution.

Thanks in advance.

4

2 回答 2

4

只需使用OrderByDescending+ ThenByDescending

var orderedObjects = objects
    .OrderByDescending(o => o.Group == keyObj.Group)
    .ThenByDescending(o => o.Country == keyObj.Country)
    .ThenByDescending(o => o.Service == keyObj.Service)
    .ThenByDescending(o => o.Industry == keyObj.Industry)
    .ThenByDescending(o => o.Technology == keyObj.Technology)
    .ThenBy(o => o.Group) // now start ordering by the properties itself
    .ThenBy(o => o.Country)
    .ThenBy(o => o.Service)
    .ThenBy(o => o.Industry)
    .ThenBy(o => o.Technology)
    .ToArray();

比较返回trueorfalse而 whiletrue比 大false,因此Descending我们首先要匹配属性。

于 2013-07-12T11:38:31.957 回答
1

用不同的权重给每场比赛打分怎么样?

一场小组赛可以是100分,一个国家1000,一个服务10000......

然后按分数对项目进行排序。

于 2013-07-12T11:40:21.660 回答