// Copyright (c) MudBlazor 2021 // MudBlazor licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. using System.Linq.Expressions; using System.Reflection; using Connected.Components; namespace Connected.Utilities; public static class SortingAssistent { public static void UpdateOrder(this IEnumerable items, ItemDropInfo dropInfo, Expression> valueUpdater, int zoneOffset = 0) { var memberSelectorExpression = valueUpdater.Body as MemberExpression; if (memberSelectorExpression == null) { throw new InvalidOperationException(); } var property = memberSelectorExpression.Member as PropertyInfo; if (property == null) { throw new InvalidOperationException(); } var newIndex = dropInfo.IndexInZone + zoneOffset; var item = dropInfo.Item; int index = 0; foreach (var _item in items.OrderBy(x => (int)property.GetValue(x))) { if (_item.Equals(item) == true) { property.SetValue(item, newIndex); } else { if (index == newIndex) { index++; } property.SetValue(_item, index); index++; } } } }