Windows Azure Cloud Storage ermöglicht es Ihnen bereits ab 0,10€ pro GB/Monat die Vorteile der Cloud zu nutzen.
Willkommen bei dotnet-snippets.de! Snippet hinzufügen Login Registrieren
Snippets in der Datenbank: 1552 | Anzahl registrierter User: 1849 | Besucher online: 338
Hauptmenü
Home
Top Ten
Zufälliger Snippet
FAQs
.NET Community
dotnet-forum.de
dotnet-kicks.de
Social

RSS Feeds
Rss Alle Snippets
Rss C#
Rss VB.NET
Rss C++
Rss ASP.NET
Partner
Member of Microsoft Community Leader/Insider Program (CLIP)

SortingHelper - MoveUpInList & MoveDownInList


Autor: codeteq
Sprache: C#
Bewertung:
noch nicht bewertet
Anzahl der Aufrufe: 1207
  
Kick it on dotnet-kicks.de  

Beschreibung:

A simple helper to perform MoveUp and MoveDown on an array of ISortable's

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



C#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
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;
        }
    }
}

Sie haben Fragen zu diesem Snippet oder brauchen Hilfe bei der .NET Entwicklung?
Freundliche und kompetente Entwickler helfen Ihnen gern weiter im Forum für .NET Entwicklung.



Kommentare:
(Zum Schreiben von Kommentaren bitte anmelden.)



Diese Snippets könnten für Sie interessant sein:
[C#] Generische Between Methode
[C#] Generische Klasse die Objekte für eine ComboBox kapselt
[C#] DelayedEventHandler
[VB.NET] Doppelte Einträge aus Arraylist löschen
[C#] Convert an ArrayList into an array
[C#] String in Byte Array und zurück wandeln
[C#] Bitmap in Byte Array schreiben
[C#] Arraylist in Textdatei schreiben
[C#] Wandelt ein Bytearray in einen Binärstring
[C#] BubbleSort Verfahren / Algorithmus
[VB.NET] Dateien nach Datum sortieren
[C#] Typ der Elemente in einem Array herausfinden
[VB.NET] Rows eines DataGridView in ein String-Array konvertieren
[VB.NET] Doppelte Arrayeinträge mit linearem Aufwand entfernen
[VB.NET] String in Byte Array und zurück wandeln
[VB.NET] Listen, Arrays, Strings konvertieren
[VB.NET] Textdatei lesen und schreiben (String, StringArray, etc...)
[VB.NET] List all controls by using the name of the controls
[C#] Datei einlesen und als ArrayList jede Zeile zurückliefern
[C#] Eine allgemeine Liste (IEnumerable) in ein Array umwandeln
[C#] long in ein byte-Array kopieren
[C#] Byte-Array in Struktur kopieren
[C#] Array zur Laufzeit erweitern
[C#] Kleine Array-Erweiterung
[VB.NET] Doppelte Eintäge löschen
[C#] Einträge in ListView verschieben
[C#] Duplikate aus einer IEnumerable entfernen
[C#] Zufallszahlen, die sich nicht wiederholen
[C#] SortableBindingList
[C#] Liste mit int array zu Liste mit string array
[VB.NET] Berechnet den Durchschnittswert von grossen Zahlenlisten
[C#] Geschachtelte Methode zur String-Bearbeitung
[VB.NET] File List
[C#] Liste durchsuchen
[C#] SystemIconsImageListWrapper
[C#] WatchedList
[C#] C# Ordner auslesen und in Liste speichern "rekursiv"
[C#] Sort and search an ArrayList.
[C#] BindingList mit Sortierfunktion

schlecht sehr gut
1 2 3 4 5 6 7 8 9 10
Nur angemeldete User können Snippets bewerten.