通过数组</int []>中的特定字段在List <int []>中查找最大值

时间:2013-11-21 06:00:56

标签: c# list max

我有一个int []列表,其中每个数组实际上有两个项目,例如

  // List of int[] each of them is a pair of ints
  List<int[]> list = new List<int[]>() {
    new int[] {1, 2},
    new int[] {3, 4},
    new int[] {5, 8}, // <- A pair with the maximum 1st value (5) to be found
    new int[] {0, 9}
  }

如何查找具有最大第一个值的列表项?我尝试过使用Linq:

list.Max(item => item[0]); // <- returns "5" when I need "int[] {5, 8}"  

但它只返回一个整数值,即item [0],当我需要整个数组时 回。谢谢!

5 个答案:

答案 0 :(得分:2)

如果最大对由两个值的总和定义,请使用以下

var max = list.OrderByDescending(i => i[0] + i[1]).First();

答案 1 :(得分:1)

您可以尝试创建 ArgMax 函数(Linq尚未提供)作为扩展方法:

  public static class EnumerableExtensions {
    // Arg Max 
    public static T ArgMax<T>(this IEnumerable<T> source, Func<T, int> selector) {
      if (Object.ReferenceEquals(null, source))
        throw new ArgumentNullException("source");

      if (Object.ReferenceEquals(null, selector))
        throw new ArgumentNullException("selector");

      T maxValue = default(T);
      int max = 0;
      Boolean assigned = false;

      foreach (T item in source) {
        int v = selector(item);

        if ((v > max) || (!assigned)) {
          assigned = true;
          max = v;
          maxValue = item;
        }
      }

      return maxValue;
    }
  }

然后使用它:

  // Test data
  List<int[]> list = new List<int[]>() {
    new int[] {1, 2},
    new int[] {3, 4},
    new int[] {5, 1}, // <- Pair with the maximum 1st value (5) to be found
    new int[] {0, 0}
  };

  int[] max = list.ArgMax(item => item[0]); // max is int[] {5, 1} array

答案 2 :(得分:0)

这似乎对我有用

List<int[]> a = new List<int[]>(){new int[]{5,2}, new int[]{3,4}};

int max= a.Max(i => i[0]);

int index = a.FindIndex(b => b[0] == max);

这将为您提供具有最大第一个值的对的索引。

然后你可以像这样访问它

int[] myMaxPair = a[index];

答案 3 :(得分:0)

使用汇总

var r = list.Aggregate((x, y) => x[0] > y[0] ? x : y);

答案 4 :(得分:0)

我将这样重写:

enum NavigationRoute {
    case like(userID: Int)
    case friendBirthday(friendId: Int)

    // MARK: - Initializers

    init?(notificationPayload: Any) {
        //
    }
}


class Test: UIViewController {

    // MARK: - Vars

    fileprivate var route: NavigationRoute?

    // MARK: - Public

    func configureWithRoute(_ route: NavigationRoute) {
        self.route = route
    }

    // MARK: - Lifecycle

    override func viewDidLoad() {
        super.viewDidLoad()

        if let eventRoute = route {
            print("do smth with your push notification logic")
        }
    }
}

let route = NavigationRoute(notificationPayload: 12)
let vc = Test()
vc.configureWithRoute(route)
vc.view // enforce VC to load view, just for testing 

收件人:

// List of int[] each of them is a pair of ints
List<int[]> list = new List<int[]>() {
new int[] {1, 2},
new int[] {3, 4},
new int[] {5, 8}, // <- A pair with the maximum 1st value (5) to be found
new int[] {0, 9}
}

您还可以使用MoreLinq库中的IList<Tuple<int, int>> list = new List<Tuple<int, int>>(); pairs.Add(Tuple.Create(1, 2)); pairs.Add(Tuple.Create(3, 4)); pairs.Add(Tuple.Create(5, 8)); pairs.Add(Tuple.Create(0, 9)); Console.WriteLine(list.OrderByDescending(i => i.Item1).First()); // prints (5, 8)