A simple stable sorting routine - far from being efficient, only for small collections. : Sort « Collections Data Structure « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Date Time
8.Design Patterns
9.Development Class
10.Event
11.File Stream
12.Generics
13.GUI Windows Form
14.Internationalization I18N
15.Language Basics
16.LINQ
17.Network
18.Office
19.Reflection
20.Regular Expressions
21.Security
22.Services Event
23.Thread
24.Web Services
25.Windows
26.Windows Presentation Foundation
27.XML
28.XML LINQ
C# / C Sharp » Collections Data Structure » SortScreenshots 
A simple stable sorting routine - far from being efficient, only for small collections.
  

#region License

/*
 * Copyright  2002-2005 the original author or authors.
 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 
 *      http://www.apache.org/licenses/LICENSE-2.0
 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#endregion

#region Imports

using System;
using System.Collections;
using System.Reflection;

#endregion

namespace Spring.Util
{
    /// <summary>
    /// Miscellaneous collection utility methods.
    /// </summary>
    /// <remarks>
    /// Mainly for internal use within the framework.
    /// </remarks>
    /// <author>Mark Pollack (.NET)</author>
    public sealed class CollectionUtils
    {
        /// <summary>
        /// A callback method used for comparing to items.
        /// </summary>
        /// <remarks>
        /// </remarks>
        /// <param name="left">the first object to compare</param>
        /// <param name="right">the second object to compare</param>
        /// <returns>Value Condition Less than zero x is less than y. Zero x equals y. Greater than zero x is greater than y.</returns>
        /// <seealso cref="IComparer.Compare"/>
        /// <seealso cref="CollectionUtils.StableSort(IEnumerable,CompareCallback)"/>
        public delegate int CompareCallback(object left, object right);

        /// <summary>
        /// A simple stable sorting routine - far from being efficient, only for small collections.
        /// </summary>
        /// <param name="input"></param>
        /// <param name="comparer"></param>
        /// <returns></returns>
        public static ICollection StableSort(IEnumerable input, IComparer comparer)
        {
            return StableSort(input, new CompareCallback(comparer.Compare));
        }

        /// <summary>
        /// A simple stable sorting routine - far from being efficient, only for small collections.
        /// </summary>
        /// <remarks>
        /// Sorting is not(!) done in-place. Instead a sorted copy of the original input is returned.
        /// </remarks>
        /// <param name="input">input collection of items to sort</param>
        /// <param name="comparer">the <see cref="CompareCallback"/> for comparing 2 items in <paramref name="input"/>.</param>
        /// <returns>a new collection of stable sorted items.</returns>
        public static ICollection StableSort(IEnumerable input, CompareCallback comparer)
        {
            ArrayList ehancedInput = new ArrayList();
            IEnumerator it = input.GetEnumerator();
            int index = 0;
            while (it.MoveNext())
            {
                ehancedInput.Add(new Entry(index, it.Current));
                index++;
            }

            ehancedInput.Sort(Entry.GetComparer(comparer));

            for (int i = 0; i < ehancedInput.Count; i++)
            {
                ehancedInput[i((Entry)ehancedInput[i]).Value;
            }

            return ehancedInput;
        }

        /// <summary>
        /// A simple stable sorting routine - far from being efficient, only for small collections.
        /// </summary>
        /// <remarks>
        /// Sorting is not(!) done in-place. Instead a sorted copy of the original input is returned.
        /// </remarks>
        /// <param name="input">input collection of items to sort</param>
        /// <param name="comparer">the <see cref="IComparer"/> for comparing 2 items in <paramref name="input"/>.</param>
        /// <returns>a new collection of stable sorted items.</returns>
        public static void StableSortInPlace(IList input, IComparer comparer)
        {
            StableSortInPlace(input, new CompareCallback(comparer.Compare));
        }

        /// <summary>
        /// A simple stable sorting routine - far from being efficient, only for small collections.
        /// </summary>
        /// <remarks>
        /// Sorting is not(!) done in-place. Instead a sorted copy of the original input is returned.
        /// </remarks>
        /// <param name="input">input collection of items to sort</param>
        /// <param name="comparer">the <see cref="CompareCallback"/> for comparing 2 items in <paramref name="input"/>.</param>
        /// <returns>a new collection of stable sorted items.</returns>
        public static void StableSortInPlace(IList input, CompareCallback comparer)
        {
            ArrayList ehancedInput = new ArrayList();
            IEnumerator it = input.GetEnumerator();
            int index = 0;
            while (it.MoveNext())
            {
                ehancedInput.Add(new Entry(index, it.Current));
                index++;
            }

            ehancedInput.Sort(Entry.GetComparer(comparer));

            for (int i = 0; i < ehancedInput.Count; i++)
            {
                input[i((Entry)ehancedInput[i]).Value;
            }
        }

        #region StableSort Utility Classes

        private class Entry
        {
            private class EntryComparer : IComparer
            {
                private readonly CompareCallback innerComparer;

                public EntryComparer(CompareCallback innerComparer)
                {
                    this.innerComparer = innerComparer;
                }

                public int Compare(object x, object y)
                {
                    Entry ex = (Entry)x;
                    Entry ey = (Entry)y;
                    int result = innerComparer(ex.Value, ey.Value);
                    if (result == 0)
                    {
                        result = ex.Index.CompareTo(ey.Index);
                    }
                    return result;
                }
            }

            public static IComparer GetComparer(CompareCallback innerComparer)
            {
                return new EntryComparer(innerComparer);
            }

            public readonly int Index;
            public readonly object Value;

            public Entry(int index, object value)
            {
                Index = index;
                Value = value;
            }
        }

        #endregion
    }
}

   
    
  
Related examples in the same category
1.Implements the recursive merge sort algorithm to sort an arrayImplements the recursive merge sort algorithm to sort an array
2.Sorts an array of data using the insertion sort algorithmSorts an array of data using the insertion sort algorithm
3.Bubble sortBubble sort
4.A simple version of the QuicksortA simple version of the Quicksort
5.Demonstrate the Bubble sortDemonstrate the Bubble sort
6.Insert Sort
7.Quick Sort
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.