按几种条件排序或搜索

时间:2013-09-20 16:22:02

标签: c#

我有一个具有属性的类StationInfo:

public uint EnergyConsumption { get; private set; }
public uint CurrentEnergyAmount { get; private set; }
public uint NumRobotsNearStation { get; private set; }
public uint NumStationsAround { get; private set; }

我正在创建一个StationInfo类型的数组。

在这个数组中,我需要找到一个同时满足以下条件的对象:

EnergyConsumption -> min
CurrentEnergyAmount -> max
NumRobotsNearStation -> min
NumStationsAround -> max

带有2个参数的示例:

{2,1}
{3,4}
{6,7}
{1,10}
{2, 15}

需要找到{min, max}

没有具有此条件的对象,但存在具有接近最小值和最大值的对象为{2, 15}

有C#交钥匙解决方案来解决这个问题吗?

4 个答案:

答案 0 :(得分:0)

怎么样:

var filtered = myArray.Where(a => a.EnergyConsumption == min 
   && a.CurrentEnergyAmount == max
   && a.NumRobotNearStation == max
   && a.NumStationsAround == min)

答案 1 :(得分:0)

电台不可能同时满足所有这些标准。返回满足至少一个标准的所有站点可能更有意义。

uint minEnergyConsumption = stations.Min(s => s.EnergyConsumption);
uint maxCurrentEnergyAmount = stations.Max(s => s.CurrentEnergyAmount);
uint maxNumRobotsNearStation = stations.Max(s => s.NumRobotsNearStation);
uint minNumStationsAround = stations.Min(s => s.NumStationsAround);

var result = stations
    .Where(s => s.EnergyConsumption == minEnergyConsumption ||
                s.CurrentEnergyAmount == maxCurrentEnergyAmount ||
                s.NumRobotNearStation == maxNumRobotsNearStation ||
                s.NumStationsAround == minNumStationsAround);

答案 2 :(得分:0)

您可以根据自己的条件创建自己的Comparer进行排序:

stations.Sort( new StationsComparer() );

其中StationsComparer是:

class StationsComparer: IComparer<StationInfo>
{
    public int Compare( StationInfo x, StationInfo y )
    {
        int result = x.EnergyConsumption.CompareTo( y.EnergyConsumption );

        if( result == 0 )
            result = y.CurrentEnergyAmount.CompareTo( x.CurrentEnergyAmount );

        if( result == 0 )
            result = y.NumRobotNearStation.CompareTo( x.NumRobotNearStation );

        if( result == 0 )
            result = x.NumStationsAround.CompareTo( y.NumStationsAround );

        return result;
    }
}

答案 3 :(得分:0)

我认为这种双程解决方案与您一样好。

  • 1次传递以查找每个有趣属性的最小值
  • 1次传递以找到匹配的项目(如果有的话)

    StationInfo FindMin( IEnumerable<StationInfo> stations )
    {
      uint minEnergyConsumption    = uint.MaxValue ;
      uint maxCurrentEnergyAmount  = uint.MinValue ;
      uint minNumRobotsNearStation = uint.MaxValue ;
      uint maxNumStationsAround    = uint.MinValue ;
    
      // find the min value for each interesting field
      foreach ( StationInfo si in stations )
      {
        minEnergyConsumption    = si.EnergyConsumption    < minEnergyConsumption    ? si.EnergyConsumption    : minEnergyConsumption    ;
        maxCurrentEnergyAmount  = si.CurrentEnergyAmount  > maxCurrentEnergyAmount  ? si.CurrentEnergyAmount  : maxCurrentEnergyAmount  ;
        minNumRobotsNearStation = si.NumRobotsNearStation < minNumRobotsNearStation ? si.NumRobotsNearStation : minNumRobotsNearStation ;
        maxNumStationsAround    = si.NumStationsAround    > maxNumStationsAround    ? si.NumStationsAround    : maxNumStationsAround    ;
      }
    
      StationInfo instance = stations.FirstOrDefault( x =>
           x.EnergyConsumption    == minEnergyConsumption 
        && x.CurrentEnergyAmount  == maxCurrentEnergyAmount
        && x.NumRobotsNearStation == minNumRobotsNearStation
        && x.NumStationsAround    == maxNumStationsAround
      );
      return instance ;
    }