/** * 排序汇总 * */ var result = Enumerable.From(vm.productList).GroupBy("$.goods_id", null, function (key, g) { var result = { currency: key, total: g.Sum(c => parseInt(c.num == " " ? 0 : c.num)) } return result; }).ToArray(); /** * FirstOrDefault 获取第一个 * */ vm.productList.forEach(function (item) { var arrRes = Enumerable.From(result).FirstOrDefault(-1,x => x.currency == item.goods_id); if (arrRes != "" && arrRes != null && arrRes != undefined && arrRes.currency == item.goods_id) { item.FOrderByNum = parseInt(arrRes.total) ; } }) //排序 var productList = Enumerable.From(vm.productList).OrderByDescending("x=>x.FOrderByNum").ToArray();//降序OrderByDescending()
最后实现的效果就是:按照最大的数量从上往下排序
linq to js 用法