├── README.md ├── OrderList ├── bin │ └── Debug │ │ ├── OrderList.exe │ │ ├── OrderList.pdb │ │ ├── OrderList.vshost.exe │ │ ├── OrderList.exe.config │ │ └── OrderList.vshost.exe.config ├── obj │ └── Debug │ │ ├── OrderList.exe │ │ ├── OrderList.pdb │ │ ├── DesignTimeResolveAssemblyReferencesInput.cache │ │ ├── OrderList.csprojResolveAssemblyReference.cache │ │ └── OrderList.csproj.FileListAbsolute.txt ├── App.config ├── MyClass.cs ├── Properties │ └── AssemblyInfo.cs ├── OrderList.csproj ├── OrderListExtensions.cs ├── RepositoryMyClass.cs └── Program.cs └── OrderList.sln /README.md: -------------------------------------------------------------------------------- 1 | # OrderList 2 | Some operations in IList. Like move element, nested elements. 3 | -------------------------------------------------------------------------------- /OrderList/bin/Debug/OrderList.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chasoliveira/OrderList/master/OrderList/bin/Debug/OrderList.exe -------------------------------------------------------------------------------- /OrderList/bin/Debug/OrderList.pdb: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chasoliveira/OrderList/master/OrderList/bin/Debug/OrderList.pdb -------------------------------------------------------------------------------- /OrderList/obj/Debug/OrderList.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chasoliveira/OrderList/master/OrderList/obj/Debug/OrderList.exe -------------------------------------------------------------------------------- /OrderList/obj/Debug/OrderList.pdb: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chasoliveira/OrderList/master/OrderList/obj/Debug/OrderList.pdb -------------------------------------------------------------------------------- /OrderList/bin/Debug/OrderList.vshost.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chasoliveira/OrderList/master/OrderList/bin/Debug/OrderList.vshost.exe -------------------------------------------------------------------------------- /OrderList/obj/Debug/DesignTimeResolveAssemblyReferencesInput.cache: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chasoliveira/OrderList/master/OrderList/obj/Debug/DesignTimeResolveAssemblyReferencesInput.cache -------------------------------------------------------------------------------- /OrderList/obj/Debug/OrderList.csprojResolveAssemblyReference.cache: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chasoliveira/OrderList/master/OrderList/obj/Debug/OrderList.csprojResolveAssemblyReference.cache -------------------------------------------------------------------------------- /OrderList/App.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /OrderList/bin/Debug/OrderList.exe.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /OrderList/bin/Debug/OrderList.vshost.exe.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /OrderList/obj/Debug/OrderList.csproj.FileListAbsolute.txt: -------------------------------------------------------------------------------- 1 | D:\Documents\Visual Studio 2015\Projects\OrderList\OrderList\bin\Debug\OrderList.exe.config 2 | D:\Documents\Visual Studio 2015\Projects\OrderList\OrderList\bin\Debug\OrderList.exe 3 | D:\Documents\Visual Studio 2015\Projects\OrderList\OrderList\bin\Debug\OrderList.pdb 4 | D:\Documents\Visual Studio 2015\Projects\OrderList\OrderList\obj\Debug\OrderList.csprojResolveAssemblyReference.cache 5 | D:\Documents\Visual Studio 2015\Projects\OrderList\OrderList\obj\Debug\OrderList.exe 6 | D:\Documents\Visual Studio 2015\Projects\OrderList\OrderList\obj\Debug\OrderList.pdb 7 | -------------------------------------------------------------------------------- /OrderList.sln: -------------------------------------------------------------------------------- 1 | 2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 14 4 | VisualStudioVersion = 14.0.24720.0 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OrderList", "OrderList\OrderList.csproj", "{0D6C5D1E-3C61-4DE2-AE81-C4044A0C9485}" 7 | EndProject 8 | Global 9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 10 | Debug|Any CPU = Debug|Any CPU 11 | Release|Any CPU = Release|Any CPU 12 | EndGlobalSection 13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 14 | {0D6C5D1E-3C61-4DE2-AE81-C4044A0C9485}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 15 | {0D6C5D1E-3C61-4DE2-AE81-C4044A0C9485}.Debug|Any CPU.Build.0 = Debug|Any CPU 16 | {0D6C5D1E-3C61-4DE2-AE81-C4044A0C9485}.Release|Any CPU.ActiveCfg = Release|Any CPU 17 | {0D6C5D1E-3C61-4DE2-AE81-C4044A0C9485}.Release|Any CPU.Build.0 = Release|Any CPU 18 | EndGlobalSection 19 | GlobalSection(SolutionProperties) = preSolution 20 | HideSolutionNode = FALSE 21 | EndGlobalSection 22 | EndGlobal 23 | -------------------------------------------------------------------------------- /OrderList/MyClass.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace OrderList 4 | { 5 | public class MyClass 6 | { 7 | public int Index { get; set; } 8 | public string Name { get; set; } 9 | public MyClass Parent { get; set; } 10 | public virtual int ParentCount { get { return GetParentCount(); } } 11 | public virtual bool HasParent { get { return Parent != null; } } 12 | 13 | public IList Nesteds { get; set; } 14 | public MyClass() 15 | { 16 | Nesteds = new List(); 17 | } 18 | private int GetParentCount() 19 | { 20 | var count = 0; 21 | GetParentCount(this, ref count); 22 | return count; 23 | } 24 | 25 | private void GetParentCount(MyClass question, ref int count) 26 | { 27 | if (question == null || !question.HasParent) return; 28 | count++; 29 | GetParentCount(question.Parent, ref count); 30 | } 31 | } 32 | 33 | public enum TypeMoviment 34 | { 35 | Root, 36 | Nested 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /OrderList/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("OrderList")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("OrderList")] 13 | [assembly: AssemblyCopyright("Copyright © 2016")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("0d6c5d1e-3c61-4de2-ae81-c4044a0c9485")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /OrderList/OrderList.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | {0D6C5D1E-3C61-4DE2-AE81-C4044A0C9485} 8 | Exe 9 | Properties 10 | OrderList 11 | OrderList 12 | v4.5.2 13 | 512 14 | true 15 | 16 | 17 | AnyCPU 18 | true 19 | full 20 | false 21 | bin\Debug\ 22 | DEBUG;TRACE 23 | prompt 24 | 4 25 | 26 | 27 | AnyCPU 28 | pdbonly 29 | true 30 | bin\Release\ 31 | TRACE 32 | prompt 33 | 4 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 63 | -------------------------------------------------------------------------------- /OrderList/OrderListExtensions.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Linq.Expressions; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | 8 | namespace OrderList 9 | { 10 | public static class OrderListExtensions 11 | { 12 | public static void Each(this IEnumerable enumerable, Action handler) 13 | { 14 | foreach (T item in enumerable) 15 | handler(item); 16 | } 17 | public static void Each(this IEnumerable enumerable, Action handler) 18 | { 19 | int idx = 0; 20 | foreach (T item in enumerable) 21 | handler(item, idx++); 22 | } 23 | 24 | public static void EachRecursive(this IEnumerable source, Action handler) 25 | { 26 | var tSource = source.ToList(); 27 | var typeSource = tSource.GetType(); 28 | int idx = 0; 29 | foreach (var item in tSource) 30 | { 31 | var thereIsNetedList = item.GetType().GetProperties().FirstOrDefault(p => p.PropertyType.Equals(typeof(IList))); 32 | handler(item, idx++); 33 | if (thereIsNetedList == null) continue; 34 | var valuesNesteds = thereIsNetedList.GetValue(item) as IList; 35 | valuesNesteds.EachRecursive(handler); 36 | } 37 | } 38 | public static bool InRange(this int source, int start, int end) 39 | { 40 | return source >= start & source <= end; 41 | } 42 | public static void Move(this IList source, int oldIndex, int newIndex, Action handler = null) 43 | { 44 | var count = source.Count; 45 | if (!oldIndex.InRange(0, count) || !newIndex.InRange(0, count)) 46 | throw new IndexOutOfRangeException(); 47 | if (oldIndex == newIndex) return; 48 | 49 | var item = source.ElementAt(oldIndex); 50 | source.RemoveAt(oldIndex); 51 | if (newIndex > oldIndex) newIndex--; 52 | source.Insert(newIndex, item); 53 | if (handler != null) 54 | source.Each(handler); 55 | } 56 | 57 | public static List ToFullList(this IEnumerable source) 58 | { 59 | var tSource = source.ToList(); 60 | if (!tSource.Any()) return tSource; 61 | 62 | var newSource = new List(); 63 | var typeSource = tSource.GetType().FullName; 64 | 65 | foreach (var item in tSource) 66 | { 67 | var newItem = item; 68 | var internalSource = newItem.GetType().GetProperties().FirstOrDefault(p => p.PropertyType == typeof(IList)); 69 | if (internalSource == null) 70 | throw new ArgumentNullException("There is no property of type as list " + typeof(TSource).Name); 71 | newSource.Add(newItem); 72 | 73 | var internalValue = internalSource.GetValue(item) as IList; 74 | if (internalValue == null || !internalValue.Any()) continue; 75 | 76 | newSource.AddRange(internalValue.ToFullList()); 77 | } 78 | return newSource.ToList(); 79 | } 80 | 81 | public static List ToFitList(this IEnumerable source, object property = null) 82 | { 83 | var typeTSouce = typeof(TSource); 84 | 85 | var sourcelevel = source.Where(p => p.GetType().GetProperties().First(v => v.PropertyType.Equals(typeTSouce)).GetValue(p) == property).ToList(); 86 | if (!sourcelevel.Any()) return sourcelevel; 87 | 88 | var newSource = new List(); 89 | newSource.AddRange(sourcelevel); 90 | 91 | foreach (var item in newSource) 92 | { 93 | var internalSource = item.GetType().GetProperties().FirstOrDefault(p => p.PropertyType == typeof(IList)); 94 | if (internalSource == null) 95 | throw new ArgumentNullException("There is no property of type as list " + typeTSouce.Name); 96 | 97 | var internaList = source.ToFitList(item); 98 | if (internaList == null || !internaList.Any()) continue; 99 | 100 | internalSource.SetValue(item, internaList.ToList()); 101 | } 102 | return newSource.ToList(); 103 | } 104 | } 105 | } 106 | -------------------------------------------------------------------------------- /OrderList/RepositoryMyClass.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | 5 | namespace OrderList 6 | { 7 | public class RepositoryMyClass 8 | { 9 | private IList _myClassList; 10 | public RepositoryMyClass() 11 | { 12 | _myClassList = new List(); 13 | SetList(); 14 | } 15 | public IList All() 16 | { 17 | var items = _myClassList.ToFullList(); 18 | return items; 19 | } 20 | 21 | public IList AllToFit() 22 | { 23 | return All().ToFitList(); 24 | } 25 | private void Updateindex() 26 | { 27 | All().Each((m, i) => m.Index = i); 28 | } 29 | public void Add(MyClass item) 30 | { 31 | _myClassList.Add(item); 32 | Updateindex(); 33 | } 34 | public void Add(int position, MyClass item) 35 | { 36 | var getItem = ElementAt(position); 37 | item.Parent = getItem; 38 | getItem.Nesteds.Add(item); 39 | _myClassList.EachRecursive((m, i) => m.Index = i); 40 | } 41 | public void Remove(MyClass item) 42 | { 43 | All().Remove(item); 44 | Updateindex(); 45 | } 46 | 47 | public void RemoveAll() 48 | { 49 | _myClassList = new List(); 50 | } 51 | internal MyClass ElementAt(int position) 52 | { 53 | return All().ElementAt(position); 54 | } 55 | 56 | internal void Move(int oldindex, int newIndex, TypeMoviment typeMov) 57 | { 58 | if (oldindex == newIndex) return; 59 | var itemOnIndex = ElementAt(newIndex); 60 | var oldItem = ElementAt(oldindex); 61 | if (typeMov == TypeMoviment.Root) 62 | { 63 | if (oldItem.HasParent) 64 | { 65 | oldItem.Parent.Nesteds.Remove(oldItem); 66 | oldItem.Parent = null; 67 | } 68 | else 69 | { 70 | _myClassList.Remove(oldItem); 71 | _myClassList.Insert(itemOnIndex.Index, oldItem); 72 | } 73 | } 74 | if (typeMov == TypeMoviment.Nested) 75 | { 76 | if (!oldItem.HasParent) 77 | _myClassList.Remove(oldItem); 78 | else 79 | oldItem.Parent.Nesteds.Remove(oldItem); 80 | 81 | oldItem.Parent = itemOnIndex; 82 | itemOnIndex.Nesteds.Add(oldItem); 83 | } 84 | _myClassList.EachRecursive((m, i) => m.Index = i); 85 | } 86 | private void SetList() 87 | { 88 | var item0 = new MyClass 89 | { 90 | Index = 0, 91 | Name = "Item 00" 92 | }; 93 | 94 | item0.Nesteds = new List { 95 | new MyClass { Index =0, Name = "Nested 00", Parent = item0}, 96 | new MyClass { Index =1, Name = "Nested 01", Parent = item0}, 97 | new MyClass { Index =2, Name = "Nested 02", Parent = item0}, 98 | }; 99 | var item1 = new MyClass 100 | { 101 | Index = 1, 102 | Name = "Item 01" 103 | }; 104 | var item00 = new MyClass { Index = 0, Name = "Nested 00", Parent = item1 }; 105 | 106 | item00.Nesteds = new List { 107 | new MyClass { Index =0, Name = "Second Nested 00", Parent = item00}, 108 | new MyClass { Index =1, Name = "Second Nested 01", Parent = item00}, 109 | }; 110 | item1.Nesteds = new List { 111 | item00, 112 | new MyClass { Index =1, Name = "Nested 01", Parent = item1}, 113 | new MyClass { Index =2, Name = "Nested 02", Parent = item1}, 114 | }; 115 | var item2 = new MyClass 116 | { 117 | Index = 2, 118 | Name = "Item 02" 119 | }; 120 | item2.Nesteds = new List { 121 | new MyClass { Index =0, Name = "Nested 00", Parent = item2}, 122 | new MyClass { Index =1, Name = "Nested 01", Parent = item2}, 123 | new MyClass { Index =2, Name = "Nested 02", Parent = item2}, 124 | }; 125 | _myClassList.Add(item0); 126 | _myClassList.Add(item1); 127 | _myClassList.Add(item2); 128 | } 129 | 130 | } 131 | } 132 | -------------------------------------------------------------------------------- /OrderList/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq; 3 | 4 | namespace OrderList 5 | { 6 | public class Program 7 | { 8 | private static RepositoryMyClass _repositoryMyClass; 9 | static void Main(string[] args) 10 | { 11 | _repositoryMyClass = new RepositoryMyClass(); 12 | 13 | Option(); 14 | Console.WriteLine("The system was finished, press any key!"); 15 | Console.ReadKey(); 16 | } 17 | 18 | private static void Option() 19 | { 20 | Console.WriteLine("Choose an Option"); 21 | Console.WriteLine("\t1 = Print List"); 22 | Console.WriteLine("\t2 = Add"); 23 | Console.WriteLine("\t3 = Add at position"); 24 | Console.WriteLine("\t4 = Add nested"); 25 | Console.WriteLine("\t5 = Move"); 26 | Console.WriteLine("\t6 = Remove Item"); 27 | Console.WriteLine("\t7 = Remove All"); 28 | Console.WriteLine("\t8 = Exit"); 29 | 30 | var choice = 1; 31 | if (!int.TryParse(Console.ReadLine(), out choice)) 32 | { 33 | Console.WriteLine("The value must be between 1 and 8!"); 34 | NewOption(); 35 | } 36 | Console.Clear(); 37 | switch (choice) 38 | { 39 | case 1: PrintMyClass(); break; 40 | case 2: AddNewMyClass(); break; 41 | case 3: AddNewQuestionAtPosition(); break; 42 | case 5: MoveItem(); break; 43 | case 4: AddNested(); break; 44 | case 6: RemoveItem(); break; 45 | case 7: _repositoryMyClass.RemoveAll(); break; 46 | case 8: return; 47 | default: 48 | break; 49 | } 50 | NewOption(); 51 | } 52 | private static void NewOption() 53 | { 54 | Console.WriteLine("...Press any key to continue!"); 55 | Console.ReadKey(); 56 | Console.Clear(); 57 | Option(); 58 | } 59 | private static string GetName() 60 | { 61 | Console.Write("Type a name: "); 62 | return Console.ReadLine(); 63 | } 64 | private static int GetPosition() 65 | { 66 | Console.Write("Type a position: "); 67 | var position = 0; 68 | int.TryParse(Console.ReadLine(), out position); 69 | return position; 70 | } 71 | private static void PrintMyClass() 72 | { 73 | Console.WriteLine("===== List as Full =====\r\n"); 74 | var myClassList = _repositoryMyClass.All(); 75 | myClassList.Each((q, i) => 76 | { 77 | q.Index = i; 78 | Console.WriteLine(string.Format("Real Index = {0:00},{1} Internal Index= {2}, Name = {3}", i, WriteSpaces(q.ParentCount), q.Index, q.Name)); 79 | }); 80 | Console.WriteLine("\r\n===== List as Fit =====\r\n"); 81 | var myClassListFit = _repositoryMyClass.AllToFit(); 82 | myClassListFit.EachRecursive((q, i) => 83 | { 84 | q.Index = i; 85 | Console.WriteLine(string.Format("Real Index = {0:00},{1} Internal Index= {2}, Name = {3}", i, WriteSpaces(q.ParentCount), q.Index, q.Name)); 86 | }); 87 | } 88 | private static void AddNewMyClass() 89 | { 90 | var name = GetName(); 91 | AddNewMyClass(name); 92 | } 93 | private static void AddNewMyClass(string name) 94 | { 95 | _repositoryMyClass.Add(new MyClass 96 | { 97 | Index = _repositoryMyClass.All().Count, 98 | Name = name, 99 | }); 100 | } 101 | private static void AddNewMyClass(string name, int position) 102 | { 103 | if (position > 0 && position < _repositoryMyClass.All().Count) 104 | { 105 | _repositoryMyClass.Add(position, new MyClass { Index = position, Name = name }); 106 | } 107 | } 108 | private static void AddNewQuestionAtPosition() 109 | { 110 | var name = GetName(); 111 | var position = GetPosition(); 112 | AddNewMyClass(name, position); 113 | } 114 | private static void AddNested() 115 | { 116 | var position = GetPosition(); 117 | var name = GetName(); 118 | var newItem = new MyClass { Name = name }; 119 | _repositoryMyClass.Add(position, newItem); 120 | } 121 | private static void RemoveItem() { } 122 | private static string WriteSpaces(int count) 123 | { 124 | var spaces = ""; 125 | for (int i = 0; i < count; i++) 126 | { 127 | spaces += "\t"; 128 | } 129 | return spaces; 130 | } 131 | 132 | private static void MoveItem() 133 | { 134 | Console.Write("Type the origin index: "); 135 | var oldindex = 0; 136 | int.TryParse(Console.ReadLine(), out oldindex); 137 | Console.Write("Type the destination index: "); 138 | var newIndex = 0; 139 | int.TryParse(Console.ReadLine(), out newIndex); 140 | Console.Write("Wich a type of moviment: 1 = root, 2 = nested: "); 141 | var type = 0; 142 | int.TryParse(Console.ReadLine(), out type); 143 | TypeMoviment typeMvt = type == 1 ? TypeMoviment.Root : TypeMoviment.Nested; 144 | _repositoryMyClass.Move(oldindex, newIndex, typeMvt); 145 | PrintMyClass(); 146 | } 147 | } 148 | } 149 | --------------------------------------------------------------------------------