Feedback

C# - SortingHelper - MoveUpInList & MoveDownInList

Veröffentlicht von am 18.11.2011
(0 Bewertungen)
A simple helper to perform MoveUp and MoveDown on an array of ISortable's
GFU-Schulungen  [Anzeige]

XML und .NET Überblick

Um auf dem neuesten Wissensstand zu sein, sollten Sie unser aktuelles ASP .NET Komplett Seminar belegen.
Nach dem Seminar kennen Sie die wichtigsten Strömungen in der Software-Technologie

ASP.NET 2.0 und 3.5

Dieser Kurs vermittelt Ihnen alle Erweiterungen von Microsoft ASP.NET 2.0. Zu diesen zählen u. a. Data Bindung, Master Pages, und Security.

using System;
using System.Collections.Generic;

namespace Sorting
{
    public interface ISortable
    {
        int Sorting { get; set; }
        List<ISortable> List { get; }
    }

    public class SortingHelper
    {

        private ISortable[] sortables;
        private List<ISortable> list;

        public SortingHelper(params ISortable[] sortables)
        {
            if (sortables.Length > 0 && sortables[0].List != null && sortables[0].List.Count > 0)
            {
                this.list = sortables[0].List;
                this.sortables = sortables;

                NumberListSerially();
            }
            else
                throw new ArgumentOutOfRangeException("sortables", sortables, "The List or the sortables itself are null or empty");
        }

        public void NumberListSerially()
        {
            //sort the list to be sure that there will be no sorting failure based on the sorting of the incoming list
            SortedList<int, ISortable> sortedList = new SortedList<int, ISortable>();
            foreach (ISortable sortable in this.list)
            {
                //while the list contains the sortingvalue -> increment the value
                while (sortedList.ContainsKey(sortable.Sorting))
                    sortable.Sorting++;

                sortedList.Add(sortable.Sorting, sortable);
            }

            //fill list with sorted "sortables"
            this.list.Clear();
            int i = 0;
            foreach (ISortable sortable in sortedList.Values)
            {
                sortable.Sorting = i++;
                this.list.Add(sortable);
            }
        }

        public void MoveUp()
        {
            Move(true);
        }

        public void MoveDown()
        {
            Move(false);
        }

        private void Move(bool up)
        {
            bool allow_numbering = false;
            //base on the direction we loop through the loop for- or backwards
            for (int s = (up) ? 0 : this.sortables.Length - 1; ((up) ? s < this.sortables.Length : s >= 0); s += ((up) ? +1 : -1))
            //foreach(ISortable sortable in this.sortables)
            {
                ISortable sortable = this.sortables[s];
                int source_index = this.list.IndexOf(sortable);
                int target_index = source_index;

                if (up)
                {
                    if (target_index <= 0)
                    {
                        for (int i = this.list.Count - 1; i > target_index; i--)
                            this.list[i].Sorting--;

                        sortable.Sorting = this.list.Count - 1;
                        break;
                    }
                    target_index--;
                    NumberListSerially();
                }
                else
                {
                    if (target_index >= this.list.Count - 1)
                    {
                        for (int i = 0; i < target_index; i++)
                            this.list[i].Sorting++;

                        sortable.Sorting = 0;
                        break;
                    }
                    target_index++;
                    NumberListSerially();
                }

                ISortable target = this.list[target_index];

                //cache sortings
                int source_sorting = sortable.Sorting;
                int target_sorting = target.Sorting;

                //move
                target.Sorting = int.MinValue;
                sortable.Sorting = target_sorting;
                target.Sorting = source_sorting;

                if (allow_numbering)
                    NumberListSerially();
            }
        }

        public int GetNewSortingValue()
        {

            return this.list.Count;
        }
    }
}

Abgelegt unter helper, sorting, array, up, down, move, list, sortable, sort.

Kommentare zum Snippet

 

Logge dich ein, um hier zu kommentieren!